Lines Matching refs:context
42 int i2c_bitbang_configure(struct i2c_bitbang *context, uint32_t dev_config) in i2c_bitbang_configure() argument
52 context->delays[T_LOW] = NS_TO_SYS_CLOCK_HW_CYCLES(4700); in i2c_bitbang_configure()
53 context->delays[T_HIGH] = NS_TO_SYS_CLOCK_HW_CYCLES(4000); in i2c_bitbang_configure()
56 context->delays[T_LOW] = NS_TO_SYS_CLOCK_HW_CYCLES(1300); in i2c_bitbang_configure()
57 context->delays[T_HIGH] = NS_TO_SYS_CLOCK_HW_CYCLES(600); in i2c_bitbang_configure()
63 context->dev_config = dev_config; in i2c_bitbang_configure()
68 int i2c_bitbang_get_config(struct i2c_bitbang *context, uint32_t *config) in i2c_bitbang_get_config() argument
70 if (context->dev_config == 0) { in i2c_bitbang_get_config()
74 *config = context->dev_config; in i2c_bitbang_get_config()
79 static void i2c_set_scl(struct i2c_bitbang *context, int state) in i2c_set_scl() argument
81 context->io->set_scl(context->io_context, state); in i2c_set_scl()
84 static void i2c_set_sda(struct i2c_bitbang *context, int state) in i2c_set_sda() argument
86 context->io->set_sda(context->io_context, state); in i2c_set_sda()
89 static int i2c_get_sda(struct i2c_bitbang *context) in i2c_get_sda() argument
91 return context->io->get_sda(context->io_context); in i2c_get_sda()
103 static void i2c_start(struct i2c_bitbang *context) in i2c_start() argument
105 if (!i2c_get_sda(context)) { in i2c_start()
110 i2c_set_scl(context, 0); in i2c_start()
111 i2c_delay(context->delays[T_LOW]); in i2c_start()
112 i2c_set_scl(context, 1); in i2c_start()
113 i2c_delay(context->delays[T_SU_STA]); in i2c_start()
115 i2c_set_sda(context, 0); in i2c_start()
116 i2c_delay(context->delays[T_HD_STA]); in i2c_start()
118 i2c_set_scl(context, 0); in i2c_start()
119 i2c_delay(context->delays[T_LOW]); in i2c_start()
122 static void i2c_repeated_start(struct i2c_bitbang *context) in i2c_repeated_start() argument
124 i2c_set_sda(context, 1); in i2c_repeated_start()
125 i2c_set_scl(context, 1); in i2c_repeated_start()
126 i2c_delay(context->delays[T_HIGH]); in i2c_repeated_start()
128 i2c_delay(context->delays[T_SU_STA]); in i2c_repeated_start()
129 i2c_start(context); in i2c_repeated_start()
132 static void i2c_stop(struct i2c_bitbang *context) in i2c_stop() argument
134 i2c_set_sda(context, 0); in i2c_stop()
135 i2c_delay(context->delays[T_LOW]); in i2c_stop()
137 i2c_set_scl(context, 1); in i2c_stop()
138 i2c_delay(context->delays[T_HIGH]); in i2c_stop()
140 i2c_delay(context->delays[T_SU_STP]); in i2c_stop()
141 i2c_set_sda(context, 1); in i2c_stop()
142 i2c_delay(context->delays[T_BUF]); /* In case we start again too soon */ in i2c_stop()
145 static void i2c_write_bit(struct i2c_bitbang *context, int bit) in i2c_write_bit() argument
148 i2c_set_sda(context, bit); in i2c_write_bit()
149 i2c_set_scl(context, 1); in i2c_write_bit()
150 i2c_delay(context->delays[T_HIGH]); in i2c_write_bit()
151 i2c_set_scl(context, 0); in i2c_write_bit()
152 i2c_delay(context->delays[T_LOW]); in i2c_write_bit()
155 static bool i2c_read_bit(struct i2c_bitbang *context) in i2c_read_bit() argument
160 i2c_set_sda(context, 1); /* Stop driving low, so slave has control */ in i2c_read_bit()
162 i2c_set_scl(context, 1); in i2c_read_bit()
163 i2c_delay(context->delays[T_HIGH]); in i2c_read_bit()
165 bit = i2c_get_sda(context); in i2c_read_bit()
167 i2c_set_scl(context, 0); in i2c_read_bit()
168 i2c_delay(context->delays[T_LOW]); in i2c_read_bit()
172 static bool i2c_write_byte(struct i2c_bitbang *context, uint8_t byte) in i2c_write_byte() argument
177 i2c_write_bit(context, byte & mask); in i2c_write_byte()
181 return !i2c_read_bit(context); in i2c_write_byte()
184 static uint8_t i2c_read_byte(struct i2c_bitbang *context) in i2c_read_byte() argument
190 byte |= i2c_read_bit(context); in i2c_read_byte()
196 int i2c_bitbang_transfer(struct i2c_bitbang *context, in i2c_bitbang_transfer() argument
212 i2c_set_scl(context, 1); in i2c_bitbang_transfer()
218 i2c_stop(context); in i2c_bitbang_transfer()
226 i2c_start(context); in i2c_bitbang_transfer()
228 i2c_repeated_start(context); in i2c_bitbang_transfer()
239 if (!i2c_write_byte(context, byte0)) { in i2c_bitbang_transfer()
251 *buf++ = i2c_read_byte(context); in i2c_bitbang_transfer()
253 i2c_write_bit(context, buf == buf_end); in i2c_bitbang_transfer()
258 if (!i2c_write_byte(context, *buf++)) { in i2c_bitbang_transfer()
272 i2c_stop(context); in i2c_bitbang_transfer()
277 int i2c_bitbang_recover_bus(struct i2c_bitbang *context) in i2c_bitbang_recover_bus() argument
298 i2c_start(context); in i2c_bitbang_recover_bus()
302 i2c_write_bit(context, 1); in i2c_bitbang_recover_bus()
306 i2c_repeated_start(context); in i2c_bitbang_recover_bus()
307 i2c_stop(context); in i2c_bitbang_recover_bus()
310 if (i2c_get_sda(context)) { in i2c_bitbang_recover_bus()
317 void i2c_bitbang_init(struct i2c_bitbang *context, in i2c_bitbang_init() argument
320 context->io = io; in i2c_bitbang_init()
321 context->io_context = io_context; in i2c_bitbang_init()
322 i2c_bitbang_configure(context, I2C_SPEED_STANDARD << I2C_SPEED_SHIFT); in i2c_bitbang_init()