mm/hmm.c: remove superfluous RCU protection around radix tree lookup
[linux/fpc-iii.git] / drivers / fsi / fsi-master-gpio.c
blob3f487449a277027390fe0b24da8ea5147a824098
1 /*
2 * A FSI master controller, using a simple GPIO bit-banging interface
3 */
5 #include <linux/crc4.h>
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/fsi.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
17 #include "fsi-master.h"
19 #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
20 #define FSI_ECHO_DELAY_CLOCKS 16 /* Number clocks for echo delay */
21 #define FSI_PRE_BREAK_CLOCKS 50 /* Number clocks to prep for break */
22 #define FSI_BREAK_CLOCKS 256 /* Number of clocks to issue break */
23 #define FSI_POST_BREAK_CLOCKS 16000 /* Number clocks to set up cfam */
24 #define FSI_INIT_CLOCKS 5000 /* Clock out any old data */
25 #define FSI_GPIO_STD_DELAY 10 /* Standard GPIO delay in nS */
26 /* todo: adjust down as low as */
27 /* possible or eliminate */
28 #define FSI_GPIO_CMD_DPOLL 0x2
29 #define FSI_GPIO_CMD_TERM 0x3f
30 #define FSI_GPIO_CMD_ABS_AR 0x4
32 #define FSI_GPIO_DPOLL_CLOCKS 100 /* < 21 will cause slave to hang */
34 /* Bus errors */
35 #define FSI_GPIO_ERR_BUSY 1 /* Slave stuck in busy state */
36 #define FSI_GPIO_RESP_ERRA 2 /* Any (misc) Error */
37 #define FSI_GPIO_RESP_ERRC 3 /* Slave reports master CRC error */
38 #define FSI_GPIO_MTOE 4 /* Master time out error */
39 #define FSI_GPIO_CRC_INVAL 5 /* Master reports slave CRC error */
41 /* Normal slave responses */
42 #define FSI_GPIO_RESP_BUSY 1
43 #define FSI_GPIO_RESP_ACK 0
44 #define FSI_GPIO_RESP_ACKD 4
46 #define FSI_GPIO_MAX_BUSY 100
47 #define FSI_GPIO_MTOE_COUNT 1000
48 #define FSI_GPIO_DRAIN_BITS 20
49 #define FSI_GPIO_CRC_SIZE 4
50 #define FSI_GPIO_MSG_ID_SIZE 2
51 #define FSI_GPIO_MSG_RESPID_SIZE 2
52 #define FSI_GPIO_PRIME_SLAVE_CLOCKS 100
54 struct fsi_master_gpio {
55 struct fsi_master master;
56 struct device *dev;
57 spinlock_t cmd_lock; /* Lock for commands */
58 struct gpio_desc *gpio_clk;
59 struct gpio_desc *gpio_data;
60 struct gpio_desc *gpio_trans; /* Voltage translator */
61 struct gpio_desc *gpio_enable; /* FSI enable */
62 struct gpio_desc *gpio_mux; /* Mux control */
63 bool external_mode;
66 #define CREATE_TRACE_POINTS
67 #include <trace/events/fsi_master_gpio.h>
69 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
71 struct fsi_gpio_msg {
72 uint64_t msg;
73 uint8_t bits;
76 static void clock_toggle(struct fsi_master_gpio *master, int count)
78 int i;
80 for (i = 0; i < count; i++) {
81 ndelay(FSI_GPIO_STD_DLY);
82 gpiod_set_value(master->gpio_clk, 0);
83 ndelay(FSI_GPIO_STD_DLY);
84 gpiod_set_value(master->gpio_clk, 1);
88 static int sda_in(struct fsi_master_gpio *master)
90 int in;
92 ndelay(FSI_GPIO_STD_DLY);
93 in = gpiod_get_value(master->gpio_data);
94 return in ? 1 : 0;
97 static void sda_out(struct fsi_master_gpio *master, int value)
99 gpiod_set_value(master->gpio_data, value);
102 static void set_sda_input(struct fsi_master_gpio *master)
104 gpiod_direction_input(master->gpio_data);
105 gpiod_set_value(master->gpio_trans, 0);
108 static void set_sda_output(struct fsi_master_gpio *master, int value)
110 gpiod_set_value(master->gpio_trans, 1);
111 gpiod_direction_output(master->gpio_data, value);
114 static void clock_zeros(struct fsi_master_gpio *master, int count)
116 set_sda_output(master, 1);
117 clock_toggle(master, count);
120 static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
121 uint8_t num_bits)
123 uint8_t bit, in_bit;
125 set_sda_input(master);
127 for (bit = 0; bit < num_bits; bit++) {
128 clock_toggle(master, 1);
129 in_bit = sda_in(master);
130 msg->msg <<= 1;
131 msg->msg |= ~in_bit & 0x1; /* Data is active low */
133 msg->bits += num_bits;
135 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
138 static void serial_out(struct fsi_master_gpio *master,
139 const struct fsi_gpio_msg *cmd)
141 uint8_t bit;
142 uint64_t msg = ~cmd->msg; /* Data is active low */
143 uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
144 uint64_t last_bit = ~0;
145 int next_bit;
147 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
149 if (!cmd->bits) {
150 dev_warn(master->dev, "trying to output 0 bits\n");
151 return;
153 set_sda_output(master, 0);
155 /* Send the start bit */
156 sda_out(master, 0);
157 clock_toggle(master, 1);
159 /* Send the message */
160 for (bit = 0; bit < cmd->bits; bit++) {
161 next_bit = (msg & sda_mask) >> (cmd->bits - 1);
162 if (last_bit ^ next_bit) {
163 sda_out(master, next_bit);
164 last_bit = next_bit;
166 clock_toggle(master, 1);
167 msg <<= 1;
171 static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
173 msg->msg <<= bits;
174 msg->msg |= data & ((1ull << bits) - 1);
175 msg->bits += bits;
178 static void msg_push_crc(struct fsi_gpio_msg *msg)
180 uint8_t crc;
181 int top;
183 top = msg->bits & 0x3;
185 /* start bit, and any non-aligned top bits */
186 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
188 /* aligned bits */
189 crc = crc4(crc, msg->msg, msg->bits - top);
191 msg_push_bits(msg, crc, 4);
195 * Encode an Absolute Address command
197 static void build_abs_ar_command(struct fsi_gpio_msg *cmd,
198 uint8_t id, uint32_t addr, size_t size, const void *data)
200 bool write = !!data;
201 uint8_t ds;
202 int i;
204 cmd->bits = 0;
205 cmd->msg = 0;
207 msg_push_bits(cmd, id, 2);
208 msg_push_bits(cmd, FSI_GPIO_CMD_ABS_AR, 3);
209 msg_push_bits(cmd, write ? 0 : 1, 1);
212 * The read/write size is encoded in the lower bits of the address
213 * (as it must be naturally-aligned), and the following ds bit.
215 * size addr:1 addr:0 ds
216 * 1 x x 0
217 * 2 x 0 1
218 * 4 0 1 1
221 ds = size > 1 ? 1 : 0;
222 addr &= ~(size - 1);
223 if (size == 4)
224 addr |= 1;
226 msg_push_bits(cmd, addr & ((1 << 21) - 1), 21);
227 msg_push_bits(cmd, ds, 1);
228 for (i = 0; write && i < size; i++)
229 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
231 msg_push_crc(cmd);
234 static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
236 cmd->bits = 0;
237 cmd->msg = 0;
239 msg_push_bits(cmd, slave_id, 2);
240 msg_push_bits(cmd, FSI_GPIO_CMD_DPOLL, 3);
241 msg_push_crc(cmd);
244 static void echo_delay(struct fsi_master_gpio *master)
246 set_sda_output(master, 1);
247 clock_toggle(master, FSI_ECHO_DELAY_CLOCKS);
250 static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
252 cmd->bits = 0;
253 cmd->msg = 0;
255 msg_push_bits(cmd, slave_id, 2);
256 msg_push_bits(cmd, FSI_GPIO_CMD_TERM, 6);
257 msg_push_crc(cmd);
261 * Store information on master errors so handler can detect and clean
262 * up the bus
264 static void fsi_master_gpio_error(struct fsi_master_gpio *master, int error)
269 static int read_one_response(struct fsi_master_gpio *master,
270 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
272 struct fsi_gpio_msg msg;
273 uint8_t id, tag;
274 uint32_t crc;
275 int i;
277 /* wait for the start bit */
278 for (i = 0; i < FSI_GPIO_MTOE_COUNT; i++) {
279 msg.bits = 0;
280 msg.msg = 0;
281 serial_in(master, &msg, 1);
282 if (msg.msg)
283 break;
285 if (i == FSI_GPIO_MTOE_COUNT) {
286 dev_dbg(master->dev,
287 "Master time out waiting for response\n");
288 fsi_master_gpio_error(master, FSI_GPIO_MTOE);
289 return -EIO;
292 msg.bits = 0;
293 msg.msg = 0;
295 /* Read slave ID & response tag */
296 serial_in(master, &msg, 4);
298 id = (msg.msg >> FSI_GPIO_MSG_RESPID_SIZE) & 0x3;
299 tag = msg.msg & 0x3;
301 /* If we have an ACK and we're expecting data, clock the data in too */
302 if (tag == FSI_GPIO_RESP_ACK && data_size)
303 serial_in(master, &msg, data_size * 8);
305 /* read CRC */
306 serial_in(master, &msg, FSI_GPIO_CRC_SIZE);
308 /* we have a whole message now; check CRC */
309 crc = crc4(0, 1, 1);
310 crc = crc4(crc, msg.msg, msg.bits);
311 if (crc) {
312 dev_dbg(master->dev, "ERR response CRC\n");
313 fsi_master_gpio_error(master, FSI_GPIO_CRC_INVAL);
314 return -EIO;
317 if (msgp)
318 *msgp = msg;
319 if (tagp)
320 *tagp = tag;
322 return 0;
325 static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
327 struct fsi_gpio_msg cmd;
328 uint8_t tag;
329 int rc;
331 build_term_command(&cmd, slave);
332 serial_out(master, &cmd);
333 echo_delay(master);
335 rc = read_one_response(master, 0, NULL, &tag);
336 if (rc < 0) {
337 dev_err(master->dev,
338 "TERM failed; lost communication with slave\n");
339 return -EIO;
340 } else if (tag != FSI_GPIO_RESP_ACK) {
341 dev_err(master->dev, "TERM failed; response %d\n", tag);
342 return -EIO;
345 return 0;
348 static int poll_for_response(struct fsi_master_gpio *master,
349 uint8_t slave, uint8_t size, void *data)
351 struct fsi_gpio_msg response, cmd;
352 int busy_count = 0, rc, i;
353 uint8_t tag;
354 uint8_t *data_byte = data;
356 retry:
357 rc = read_one_response(master, size, &response, &tag);
358 if (rc)
359 return rc;
361 switch (tag) {
362 case FSI_GPIO_RESP_ACK:
363 if (size && data) {
364 uint64_t val = response.msg;
365 /* clear crc & mask */
366 val >>= 4;
367 val &= (1ull << (size * 8)) - 1;
369 for (i = 0; i < size; i++) {
370 data_byte[size-i-1] = val;
371 val >>= 8;
374 break;
375 case FSI_GPIO_RESP_BUSY:
377 * Its necessary to clock slave before issuing
378 * d-poll, not indicated in the hardware protocol
379 * spec. < 20 clocks causes slave to hang, 21 ok.
381 clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
382 if (busy_count++ < FSI_GPIO_MAX_BUSY) {
383 build_dpoll_command(&cmd, slave);
384 serial_out(master, &cmd);
385 echo_delay(master);
386 goto retry;
388 dev_warn(master->dev,
389 "ERR slave is stuck in busy state, issuing TERM\n");
390 issue_term(master, slave);
391 rc = -EIO;
392 break;
394 case FSI_GPIO_RESP_ERRA:
395 case FSI_GPIO_RESP_ERRC:
396 dev_dbg(master->dev, "ERR%c received: 0x%x\n",
397 tag == FSI_GPIO_RESP_ERRA ? 'A' : 'C',
398 (int)response.msg);
399 fsi_master_gpio_error(master, response.msg);
400 rc = -EIO;
401 break;
404 /* Clock the slave enough to be ready for next operation */
405 clock_zeros(master, FSI_GPIO_PRIME_SLAVE_CLOCKS);
406 return rc;
409 static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
410 struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
412 unsigned long flags;
413 int rc;
415 spin_lock_irqsave(&master->cmd_lock, flags);
417 if (master->external_mode) {
418 spin_unlock_irqrestore(&master->cmd_lock, flags);
419 return -EBUSY;
422 serial_out(master, cmd);
423 echo_delay(master);
424 rc = poll_for_response(master, slave, resp_len, resp);
425 spin_unlock_irqrestore(&master->cmd_lock, flags);
427 return rc;
430 static int fsi_master_gpio_read(struct fsi_master *_master, int link,
431 uint8_t id, uint32_t addr, void *val, size_t size)
433 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
434 struct fsi_gpio_msg cmd;
436 if (link != 0)
437 return -ENODEV;
439 build_abs_ar_command(&cmd, id, addr, size, NULL);
440 return fsi_master_gpio_xfer(master, id, &cmd, size, val);
443 static int fsi_master_gpio_write(struct fsi_master *_master, int link,
444 uint8_t id, uint32_t addr, const void *val, size_t size)
446 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
447 struct fsi_gpio_msg cmd;
449 if (link != 0)
450 return -ENODEV;
452 build_abs_ar_command(&cmd, id, addr, size, val);
453 return fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
456 static int fsi_master_gpio_term(struct fsi_master *_master,
457 int link, uint8_t id)
459 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
460 struct fsi_gpio_msg cmd;
462 if (link != 0)
463 return -ENODEV;
465 build_term_command(&cmd, id);
466 return fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
469 static int fsi_master_gpio_break(struct fsi_master *_master, int link)
471 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
472 unsigned long flags;
474 if (link != 0)
475 return -ENODEV;
477 trace_fsi_master_gpio_break(master);
479 spin_lock_irqsave(&master->cmd_lock, flags);
480 if (master->external_mode) {
481 spin_unlock_irqrestore(&master->cmd_lock, flags);
482 return -EBUSY;
484 set_sda_output(master, 1);
485 sda_out(master, 1);
486 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
487 sda_out(master, 0);
488 clock_toggle(master, FSI_BREAK_CLOCKS);
489 echo_delay(master);
490 sda_out(master, 1);
491 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
492 spin_unlock_irqrestore(&master->cmd_lock, flags);
494 /* Wait for logic reset to take effect */
495 udelay(200);
497 return 0;
500 static void fsi_master_gpio_init(struct fsi_master_gpio *master)
502 gpiod_direction_output(master->gpio_mux, 1);
503 gpiod_direction_output(master->gpio_trans, 1);
504 gpiod_direction_output(master->gpio_enable, 1);
505 gpiod_direction_output(master->gpio_clk, 1);
506 gpiod_direction_output(master->gpio_data, 1);
508 /* todo: evaluate if clocks can be reduced */
509 clock_zeros(master, FSI_INIT_CLOCKS);
512 static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
514 gpiod_direction_output(master->gpio_mux, 0);
515 gpiod_direction_output(master->gpio_trans, 0);
516 gpiod_direction_output(master->gpio_enable, 1);
517 gpiod_direction_input(master->gpio_clk);
518 gpiod_direction_input(master->gpio_data);
521 static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
523 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
524 unsigned long flags;
525 int rc = -EBUSY;
527 if (link != 0)
528 return -ENODEV;
530 spin_lock_irqsave(&master->cmd_lock, flags);
531 if (!master->external_mode) {
532 gpiod_set_value(master->gpio_enable, 1);
533 rc = 0;
535 spin_unlock_irqrestore(&master->cmd_lock, flags);
537 return rc;
540 static ssize_t external_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 struct fsi_master_gpio *master = dev_get_drvdata(dev);
545 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
546 master->external_mode ? 1 : 0);
549 static ssize_t external_mode_store(struct device *dev,
550 struct device_attribute *attr, const char *buf, size_t count)
552 struct fsi_master_gpio *master = dev_get_drvdata(dev);
553 unsigned long flags, val;
554 bool external_mode;
555 int err;
557 err = kstrtoul(buf, 0, &val);
558 if (err)
559 return err;
561 external_mode = !!val;
563 spin_lock_irqsave(&master->cmd_lock, flags);
565 if (external_mode == master->external_mode) {
566 spin_unlock_irqrestore(&master->cmd_lock, flags);
567 return count;
570 master->external_mode = external_mode;
571 if (master->external_mode)
572 fsi_master_gpio_init_external(master);
573 else
574 fsi_master_gpio_init(master);
575 spin_unlock_irqrestore(&master->cmd_lock, flags);
577 fsi_master_rescan(&master->master);
579 return count;
582 static DEVICE_ATTR(external_mode, 0664,
583 external_mode_show, external_mode_store);
585 static int fsi_master_gpio_probe(struct platform_device *pdev)
587 struct fsi_master_gpio *master;
588 struct gpio_desc *gpio;
589 int rc;
591 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
592 if (!master)
593 return -ENOMEM;
595 master->dev = &pdev->dev;
596 master->master.dev.parent = master->dev;
597 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
599 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
600 if (IS_ERR(gpio)) {
601 dev_err(&pdev->dev, "failed to get clock gpio\n");
602 return PTR_ERR(gpio);
604 master->gpio_clk = gpio;
606 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
607 if (IS_ERR(gpio)) {
608 dev_err(&pdev->dev, "failed to get data gpio\n");
609 return PTR_ERR(gpio);
611 master->gpio_data = gpio;
613 /* Optional GPIOs */
614 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
615 if (IS_ERR(gpio)) {
616 dev_err(&pdev->dev, "failed to get trans gpio\n");
617 return PTR_ERR(gpio);
619 master->gpio_trans = gpio;
621 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
622 if (IS_ERR(gpio)) {
623 dev_err(&pdev->dev, "failed to get enable gpio\n");
624 return PTR_ERR(gpio);
626 master->gpio_enable = gpio;
628 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
629 if (IS_ERR(gpio)) {
630 dev_err(&pdev->dev, "failed to get mux gpio\n");
631 return PTR_ERR(gpio);
633 master->gpio_mux = gpio;
635 master->master.n_links = 1;
636 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
637 master->master.read = fsi_master_gpio_read;
638 master->master.write = fsi_master_gpio_write;
639 master->master.term = fsi_master_gpio_term;
640 master->master.send_break = fsi_master_gpio_break;
641 master->master.link_enable = fsi_master_gpio_link_enable;
642 platform_set_drvdata(pdev, master);
643 spin_lock_init(&master->cmd_lock);
645 fsi_master_gpio_init(master);
647 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
648 if (rc)
649 return rc;
651 return fsi_master_register(&master->master);
655 static int fsi_master_gpio_remove(struct platform_device *pdev)
657 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
659 devm_gpiod_put(&pdev->dev, master->gpio_clk);
660 devm_gpiod_put(&pdev->dev, master->gpio_data);
661 if (master->gpio_trans)
662 devm_gpiod_put(&pdev->dev, master->gpio_trans);
663 if (master->gpio_enable)
664 devm_gpiod_put(&pdev->dev, master->gpio_enable);
665 if (master->gpio_mux)
666 devm_gpiod_put(&pdev->dev, master->gpio_mux);
667 fsi_master_unregister(&master->master);
669 of_node_put(master->master.dev.of_node);
671 return 0;
674 static const struct of_device_id fsi_master_gpio_match[] = {
675 { .compatible = "fsi-master-gpio" },
676 { },
679 static struct platform_driver fsi_master_gpio_driver = {
680 .driver = {
681 .name = "fsi-master-gpio",
682 .of_match_table = fsi_master_gpio_match,
684 .probe = fsi_master_gpio_probe,
685 .remove = fsi_master_gpio_remove,
688 module_platform_driver(fsi_master_gpio_driver);
689 MODULE_LICENSE("GPL");