fsi-master-gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * A FSI master controller, using a simple GPIO bit-banging interface
  3. */
  4. #include <linux/crc4.h>
  5. #include <linux/delay.h>
  6. #include <linux/device.h>
  7. #include <linux/fsi.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/io.h>
  10. #include <linux/irqflags.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include "fsi-master.h"
  16. #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
  17. #define LAST_ADDR_INVALID 0x1
  18. struct fsi_master_gpio {
  19. struct fsi_master master;
  20. struct device *dev;
  21. struct mutex cmd_lock; /* mutex for command ordering */
  22. struct gpio_desc *gpio_clk;
  23. struct gpio_desc *gpio_data;
  24. struct gpio_desc *gpio_trans; /* Voltage translator */
  25. struct gpio_desc *gpio_enable; /* FSI enable */
  26. struct gpio_desc *gpio_mux; /* Mux control */
  27. bool external_mode;
  28. bool no_delays;
  29. uint32_t last_addr;
  30. uint8_t t_send_delay;
  31. uint8_t t_echo_delay;
  32. };
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/fsi_master_gpio.h>
  35. #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
  36. struct fsi_gpio_msg {
  37. uint64_t msg;
  38. uint8_t bits;
  39. };
  40. static void clock_toggle(struct fsi_master_gpio *master, int count)
  41. {
  42. int i;
  43. for (i = 0; i < count; i++) {
  44. if (!master->no_delays)
  45. ndelay(FSI_GPIO_STD_DLY);
  46. gpiod_set_value(master->gpio_clk, 0);
  47. if (!master->no_delays)
  48. ndelay(FSI_GPIO_STD_DLY);
  49. gpiod_set_value(master->gpio_clk, 1);
  50. }
  51. }
  52. static int sda_clock_in(struct fsi_master_gpio *master)
  53. {
  54. int in;
  55. if (!master->no_delays)
  56. ndelay(FSI_GPIO_STD_DLY);
  57. gpiod_set_value(master->gpio_clk, 0);
  58. /* Dummy read to feed the synchronizers */
  59. gpiod_get_value(master->gpio_data);
  60. /* Actual data read */
  61. in = gpiod_get_value(master->gpio_data);
  62. if (!master->no_delays)
  63. ndelay(FSI_GPIO_STD_DLY);
  64. gpiod_set_value(master->gpio_clk, 1);
  65. return in ? 1 : 0;
  66. }
  67. static void sda_out(struct fsi_master_gpio *master, int value)
  68. {
  69. gpiod_set_value(master->gpio_data, value);
  70. }
  71. static void set_sda_input(struct fsi_master_gpio *master)
  72. {
  73. gpiod_direction_input(master->gpio_data);
  74. gpiod_set_value(master->gpio_trans, 0);
  75. }
  76. static void set_sda_output(struct fsi_master_gpio *master, int value)
  77. {
  78. gpiod_set_value(master->gpio_trans, 1);
  79. gpiod_direction_output(master->gpio_data, value);
  80. }
  81. static void clock_zeros(struct fsi_master_gpio *master, int count)
  82. {
  83. trace_fsi_master_gpio_clock_zeros(master, count);
  84. set_sda_output(master, 1);
  85. clock_toggle(master, count);
  86. }
  87. static void echo_delay(struct fsi_master_gpio *master)
  88. {
  89. clock_zeros(master, master->t_echo_delay);
  90. }
  91. static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
  92. uint8_t num_bits)
  93. {
  94. uint8_t bit, in_bit;
  95. set_sda_input(master);
  96. for (bit = 0; bit < num_bits; bit++) {
  97. in_bit = sda_clock_in(master);
  98. msg->msg <<= 1;
  99. msg->msg |= ~in_bit & 0x1; /* Data is active low */
  100. }
  101. msg->bits += num_bits;
  102. trace_fsi_master_gpio_in(master, num_bits, msg->msg);
  103. }
  104. static void serial_out(struct fsi_master_gpio *master,
  105. const struct fsi_gpio_msg *cmd)
  106. {
  107. uint8_t bit;
  108. uint64_t msg = ~cmd->msg; /* Data is active low */
  109. uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
  110. uint64_t last_bit = ~0;
  111. int next_bit;
  112. trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
  113. if (!cmd->bits) {
  114. dev_warn(master->dev, "trying to output 0 bits\n");
  115. return;
  116. }
  117. set_sda_output(master, 0);
  118. /* Send the start bit */
  119. sda_out(master, 0);
  120. clock_toggle(master, 1);
  121. /* Send the message */
  122. for (bit = 0; bit < cmd->bits; bit++) {
  123. next_bit = (msg & sda_mask) >> (cmd->bits - 1);
  124. if (last_bit ^ next_bit) {
  125. sda_out(master, next_bit);
  126. last_bit = next_bit;
  127. }
  128. clock_toggle(master, 1);
  129. msg <<= 1;
  130. }
  131. }
  132. static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
  133. {
  134. msg->msg <<= bits;
  135. msg->msg |= data & ((1ull << bits) - 1);
  136. msg->bits += bits;
  137. }
  138. static void msg_push_crc(struct fsi_gpio_msg *msg)
  139. {
  140. uint8_t crc;
  141. int top;
  142. top = msg->bits & 0x3;
  143. /* start bit, and any non-aligned top bits */
  144. crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
  145. /* aligned bits */
  146. crc = crc4(crc, msg->msg, msg->bits - top);
  147. msg_push_bits(msg, crc, 4);
  148. }
  149. static bool check_same_address(struct fsi_master_gpio *master, int id,
  150. uint32_t addr)
  151. {
  152. /* this will also handle LAST_ADDR_INVALID */
  153. return master->last_addr == (((id & 0x3) << 21) | (addr & ~0x3));
  154. }
  155. static bool check_relative_address(struct fsi_master_gpio *master, int id,
  156. uint32_t addr, uint32_t *rel_addrp)
  157. {
  158. uint32_t last_addr = master->last_addr;
  159. int32_t rel_addr;
  160. if (last_addr == LAST_ADDR_INVALID)
  161. return false;
  162. /* We may be in 23-bit addressing mode, which uses the id as the
  163. * top two address bits. So, if we're referencing a different ID,
  164. * use absolute addresses.
  165. */
  166. if (((last_addr >> 21) & 0x3) != id)
  167. return false;
  168. /* remove the top two bits from any 23-bit addressing */
  169. last_addr &= (1 << 21) - 1;
  170. /* We know that the addresses are limited to 21 bits, so this won't
  171. * overflow the signed rel_addr */
  172. rel_addr = addr - last_addr;
  173. if (rel_addr > 255 || rel_addr < -256)
  174. return false;
  175. *rel_addrp = (uint32_t)rel_addr;
  176. return true;
  177. }
  178. static void last_address_update(struct fsi_master_gpio *master,
  179. int id, bool valid, uint32_t addr)
  180. {
  181. if (!valid)
  182. master->last_addr = LAST_ADDR_INVALID;
  183. else
  184. master->last_addr = ((id & 0x3) << 21) | (addr & ~0x3);
  185. }
  186. /*
  187. * Encode an Absolute/Relative/Same Address command
  188. */
  189. static void build_ar_command(struct fsi_master_gpio *master,
  190. struct fsi_gpio_msg *cmd, uint8_t id,
  191. uint32_t addr, size_t size, const void *data)
  192. {
  193. int i, addr_bits, opcode_bits;
  194. bool write = !!data;
  195. uint8_t ds, opcode;
  196. uint32_t rel_addr;
  197. cmd->bits = 0;
  198. cmd->msg = 0;
  199. /* we have 21 bits of address max */
  200. addr &= ((1 << 21) - 1);
  201. /* cmd opcodes are variable length - SAME_AR is only two bits */
  202. opcode_bits = 3;
  203. if (check_same_address(master, id, addr)) {
  204. /* we still address the byte offset within the word */
  205. addr_bits = 2;
  206. opcode_bits = 2;
  207. opcode = FSI_CMD_SAME_AR;
  208. trace_fsi_master_gpio_cmd_same_addr(master);
  209. } else if (check_relative_address(master, id, addr, &rel_addr)) {
  210. /* 8 bits plus sign */
  211. addr_bits = 9;
  212. addr = rel_addr;
  213. opcode = FSI_CMD_REL_AR;
  214. trace_fsi_master_gpio_cmd_rel_addr(master, rel_addr);
  215. } else {
  216. addr_bits = 21;
  217. opcode = FSI_CMD_ABS_AR;
  218. trace_fsi_master_gpio_cmd_abs_addr(master, addr);
  219. }
  220. /*
  221. * The read/write size is encoded in the lower bits of the address
  222. * (as it must be naturally-aligned), and the following ds bit.
  223. *
  224. * size addr:1 addr:0 ds
  225. * 1 x x 0
  226. * 2 x 0 1
  227. * 4 0 1 1
  228. *
  229. */
  230. ds = size > 1 ? 1 : 0;
  231. addr &= ~(size - 1);
  232. if (size == 4)
  233. addr |= 1;
  234. msg_push_bits(cmd, id, 2);
  235. msg_push_bits(cmd, opcode, opcode_bits);
  236. msg_push_bits(cmd, write ? 0 : 1, 1);
  237. msg_push_bits(cmd, addr, addr_bits);
  238. msg_push_bits(cmd, ds, 1);
  239. for (i = 0; write && i < size; i++)
  240. msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
  241. msg_push_crc(cmd);
  242. }
  243. static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
  244. {
  245. cmd->bits = 0;
  246. cmd->msg = 0;
  247. msg_push_bits(cmd, slave_id, 2);
  248. msg_push_bits(cmd, FSI_CMD_DPOLL, 3);
  249. msg_push_crc(cmd);
  250. }
  251. static void build_epoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
  252. {
  253. cmd->bits = 0;
  254. cmd->msg = 0;
  255. msg_push_bits(cmd, slave_id, 2);
  256. msg_push_bits(cmd, FSI_CMD_EPOLL, 3);
  257. msg_push_crc(cmd);
  258. }
  259. static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
  260. {
  261. cmd->bits = 0;
  262. cmd->msg = 0;
  263. msg_push_bits(cmd, slave_id, 2);
  264. msg_push_bits(cmd, FSI_CMD_TERM, 6);
  265. msg_push_crc(cmd);
  266. }
  267. /*
  268. * Note: callers rely specifically on this returning -EAGAIN for
  269. * a CRC error detected in the response. Use other error code
  270. * for other situations. It will be converted to something else
  271. * higher up the stack before it reaches userspace.
  272. */
  273. static int read_one_response(struct fsi_master_gpio *master,
  274. uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
  275. {
  276. struct fsi_gpio_msg msg;
  277. unsigned long flags;
  278. uint32_t crc;
  279. uint8_t tag;
  280. int i;
  281. local_irq_save(flags);
  282. /* wait for the start bit */
  283. for (i = 0; i < FSI_MASTER_MTOE_COUNT; i++) {
  284. msg.bits = 0;
  285. msg.msg = 0;
  286. serial_in(master, &msg, 1);
  287. if (msg.msg)
  288. break;
  289. }
  290. if (i == FSI_MASTER_MTOE_COUNT) {
  291. dev_dbg(master->dev,
  292. "Master time out waiting for response\n");
  293. local_irq_restore(flags);
  294. return -ETIMEDOUT;
  295. }
  296. msg.bits = 0;
  297. msg.msg = 0;
  298. /* Read slave ID & response tag */
  299. serial_in(master, &msg, 4);
  300. tag = msg.msg & 0x3;
  301. /* If we have an ACK and we're expecting data, clock the data in too */
  302. if (tag == FSI_RESP_ACK && data_size)
  303. serial_in(master, &msg, data_size * 8);
  304. /* read CRC */
  305. serial_in(master, &msg, FSI_CRC_SIZE);
  306. local_irq_restore(flags);
  307. /* we have a whole message now; check CRC */
  308. crc = crc4(0, 1, 1);
  309. crc = crc4(crc, msg.msg, msg.bits);
  310. if (crc) {
  311. /* Check if it's all 1's, that probably means the host is off */
  312. if (((~msg.msg) & ((1ull << msg.bits) - 1)) == 0)
  313. return -ENODEV;
  314. dev_dbg(master->dev, "ERR response CRC msg: 0x%016llx (%d bits)\n",
  315. msg.msg, msg.bits);
  316. return -EAGAIN;
  317. }
  318. if (msgp)
  319. *msgp = msg;
  320. if (tagp)
  321. *tagp = tag;
  322. return 0;
  323. }
  324. static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
  325. {
  326. struct fsi_gpio_msg cmd;
  327. unsigned long flags;
  328. uint8_t tag;
  329. int rc;
  330. build_term_command(&cmd, slave);
  331. local_irq_save(flags);
  332. serial_out(master, &cmd);
  333. echo_delay(master);
  334. local_irq_restore(flags);
  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_RESP_ACK) {
  341. dev_err(master->dev, "TERM failed; response %d\n", tag);
  342. return -EIO;
  343. }
  344. return 0;
  345. }
  346. static int poll_for_response(struct fsi_master_gpio *master,
  347. uint8_t slave, uint8_t size, void *data)
  348. {
  349. struct fsi_gpio_msg response, cmd;
  350. int busy_count = 0, rc, i;
  351. unsigned long flags;
  352. uint8_t tag;
  353. uint8_t *data_byte = data;
  354. int crc_err_retries = 0;
  355. retry:
  356. rc = read_one_response(master, size, &response, &tag);
  357. /* Handle retries on CRC errors */
  358. if (rc == -EAGAIN) {
  359. /* Too many retries ? */
  360. if (crc_err_retries++ > FSI_CRC_ERR_RETRIES) {
  361. /*
  362. * Pass it up as a -EIO otherwise upper level will retry
  363. * the whole command which isn't what we want here.
  364. */
  365. rc = -EIO;
  366. goto fail;
  367. }
  368. dev_dbg(master->dev,
  369. "CRC error retry %d\n", crc_err_retries);
  370. trace_fsi_master_gpio_crc_rsp_error(master);
  371. build_epoll_command(&cmd, slave);
  372. local_irq_save(flags);
  373. clock_zeros(master, FSI_MASTER_EPOLL_CLOCKS);
  374. serial_out(master, &cmd);
  375. echo_delay(master);
  376. local_irq_restore(flags);
  377. goto retry;
  378. } else if (rc)
  379. goto fail;
  380. switch (tag) {
  381. case FSI_RESP_ACK:
  382. if (size && data) {
  383. uint64_t val = response.msg;
  384. /* clear crc & mask */
  385. val >>= 4;
  386. val &= (1ull << (size * 8)) - 1;
  387. for (i = 0; i < size; i++) {
  388. data_byte[size-i-1] = val;
  389. val >>= 8;
  390. }
  391. }
  392. break;
  393. case FSI_RESP_BUSY:
  394. /*
  395. * Its necessary to clock slave before issuing
  396. * d-poll, not indicated in the hardware protocol
  397. * spec. < 20 clocks causes slave to hang, 21 ok.
  398. */
  399. if (busy_count++ < FSI_MASTER_MAX_BUSY) {
  400. build_dpoll_command(&cmd, slave);
  401. local_irq_save(flags);
  402. clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
  403. serial_out(master, &cmd);
  404. echo_delay(master);
  405. local_irq_restore(flags);
  406. goto retry;
  407. }
  408. dev_warn(master->dev,
  409. "ERR slave is stuck in busy state, issuing TERM\n");
  410. local_irq_save(flags);
  411. clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
  412. local_irq_restore(flags);
  413. issue_term(master, slave);
  414. rc = -EIO;
  415. break;
  416. case FSI_RESP_ERRA:
  417. dev_dbg(master->dev, "ERRA received: 0x%x\n", (int)response.msg);
  418. rc = -EIO;
  419. break;
  420. case FSI_RESP_ERRC:
  421. dev_dbg(master->dev, "ERRC received: 0x%x\n", (int)response.msg);
  422. trace_fsi_master_gpio_crc_cmd_error(master);
  423. rc = -EAGAIN;
  424. break;
  425. }
  426. if (busy_count > 0)
  427. trace_fsi_master_gpio_poll_response_busy(master, busy_count);
  428. fail:
  429. /*
  430. * tSendDelay clocks, avoids signal reflections when switching
  431. * from receive of response back to send of data.
  432. */
  433. local_irq_save(flags);
  434. clock_zeros(master, master->t_send_delay);
  435. local_irq_restore(flags);
  436. return rc;
  437. }
  438. static int send_request(struct fsi_master_gpio *master,
  439. struct fsi_gpio_msg *cmd)
  440. {
  441. unsigned long flags;
  442. if (master->external_mode)
  443. return -EBUSY;
  444. local_irq_save(flags);
  445. serial_out(master, cmd);
  446. echo_delay(master);
  447. local_irq_restore(flags);
  448. return 0;
  449. }
  450. static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
  451. struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
  452. {
  453. int rc = -EAGAIN, retries = 0;
  454. while ((retries++) < FSI_CRC_ERR_RETRIES) {
  455. rc = send_request(master, cmd);
  456. if (rc)
  457. break;
  458. rc = poll_for_response(master, slave, resp_len, resp);
  459. if (rc != -EAGAIN)
  460. break;
  461. rc = -EIO;
  462. dev_warn(master->dev, "ECRC retry %d\n", retries);
  463. /* Pace it a bit before retry */
  464. msleep(1);
  465. }
  466. return rc;
  467. }
  468. static int fsi_master_gpio_read(struct fsi_master *_master, int link,
  469. uint8_t id, uint32_t addr, void *val, size_t size)
  470. {
  471. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  472. struct fsi_gpio_msg cmd;
  473. int rc;
  474. if (link != 0)
  475. return -ENODEV;
  476. mutex_lock(&master->cmd_lock);
  477. build_ar_command(master, &cmd, id, addr, size, NULL);
  478. rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
  479. last_address_update(master, id, rc == 0, addr);
  480. mutex_unlock(&master->cmd_lock);
  481. return rc;
  482. }
  483. static int fsi_master_gpio_write(struct fsi_master *_master, int link,
  484. uint8_t id, uint32_t addr, const void *val, size_t size)
  485. {
  486. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  487. struct fsi_gpio_msg cmd;
  488. int rc;
  489. if (link != 0)
  490. return -ENODEV;
  491. mutex_lock(&master->cmd_lock);
  492. build_ar_command(master, &cmd, id, addr, size, val);
  493. rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
  494. last_address_update(master, id, rc == 0, addr);
  495. mutex_unlock(&master->cmd_lock);
  496. return rc;
  497. }
  498. static int fsi_master_gpio_term(struct fsi_master *_master,
  499. int link, uint8_t id)
  500. {
  501. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  502. struct fsi_gpio_msg cmd;
  503. int rc;
  504. if (link != 0)
  505. return -ENODEV;
  506. mutex_lock(&master->cmd_lock);
  507. build_term_command(&cmd, id);
  508. rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
  509. last_address_update(master, id, false, 0);
  510. mutex_unlock(&master->cmd_lock);
  511. return rc;
  512. }
  513. static int fsi_master_gpio_break(struct fsi_master *_master, int link)
  514. {
  515. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  516. unsigned long flags;
  517. if (link != 0)
  518. return -ENODEV;
  519. trace_fsi_master_gpio_break(master);
  520. mutex_lock(&master->cmd_lock);
  521. if (master->external_mode) {
  522. mutex_unlock(&master->cmd_lock);
  523. return -EBUSY;
  524. }
  525. local_irq_save(flags);
  526. set_sda_output(master, 1);
  527. sda_out(master, 1);
  528. clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
  529. sda_out(master, 0);
  530. clock_toggle(master, FSI_BREAK_CLOCKS);
  531. echo_delay(master);
  532. sda_out(master, 1);
  533. clock_toggle(master, FSI_POST_BREAK_CLOCKS);
  534. local_irq_restore(flags);
  535. last_address_update(master, 0, false, 0);
  536. mutex_unlock(&master->cmd_lock);
  537. /* Wait for logic reset to take effect */
  538. udelay(200);
  539. return 0;
  540. }
  541. static void fsi_master_gpio_init(struct fsi_master_gpio *master)
  542. {
  543. unsigned long flags;
  544. gpiod_direction_output(master->gpio_mux, 1);
  545. gpiod_direction_output(master->gpio_trans, 1);
  546. gpiod_direction_output(master->gpio_enable, 1);
  547. gpiod_direction_output(master->gpio_clk, 1);
  548. gpiod_direction_output(master->gpio_data, 1);
  549. /* todo: evaluate if clocks can be reduced */
  550. local_irq_save(flags);
  551. clock_zeros(master, FSI_INIT_CLOCKS);
  552. local_irq_restore(flags);
  553. }
  554. static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
  555. {
  556. gpiod_direction_output(master->gpio_mux, 0);
  557. gpiod_direction_output(master->gpio_trans, 0);
  558. gpiod_direction_output(master->gpio_enable, 1);
  559. gpiod_direction_input(master->gpio_clk);
  560. gpiod_direction_input(master->gpio_data);
  561. }
  562. static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
  563. {
  564. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  565. int rc = -EBUSY;
  566. if (link != 0)
  567. return -ENODEV;
  568. mutex_lock(&master->cmd_lock);
  569. if (!master->external_mode) {
  570. gpiod_set_value(master->gpio_enable, 1);
  571. rc = 0;
  572. }
  573. mutex_unlock(&master->cmd_lock);
  574. return rc;
  575. }
  576. static int fsi_master_gpio_link_config(struct fsi_master *_master, int link,
  577. u8 t_send_delay, u8 t_echo_delay)
  578. {
  579. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  580. if (link != 0)
  581. return -ENODEV;
  582. mutex_lock(&master->cmd_lock);
  583. master->t_send_delay = t_send_delay;
  584. master->t_echo_delay = t_echo_delay;
  585. mutex_unlock(&master->cmd_lock);
  586. return 0;
  587. }
  588. static ssize_t external_mode_show(struct device *dev,
  589. struct device_attribute *attr, char *buf)
  590. {
  591. struct fsi_master_gpio *master = dev_get_drvdata(dev);
  592. return snprintf(buf, PAGE_SIZE - 1, "%u\n",
  593. master->external_mode ? 1 : 0);
  594. }
  595. static ssize_t external_mode_store(struct device *dev,
  596. struct device_attribute *attr, const char *buf, size_t count)
  597. {
  598. struct fsi_master_gpio *master = dev_get_drvdata(dev);
  599. unsigned long val;
  600. bool external_mode;
  601. int err;
  602. err = kstrtoul(buf, 0, &val);
  603. if (err)
  604. return err;
  605. external_mode = !!val;
  606. mutex_lock(&master->cmd_lock);
  607. if (external_mode == master->external_mode) {
  608. mutex_unlock(&master->cmd_lock);
  609. return count;
  610. }
  611. master->external_mode = external_mode;
  612. if (master->external_mode)
  613. fsi_master_gpio_init_external(master);
  614. else
  615. fsi_master_gpio_init(master);
  616. mutex_unlock(&master->cmd_lock);
  617. fsi_master_rescan(&master->master);
  618. return count;
  619. }
  620. static DEVICE_ATTR(external_mode, 0664,
  621. external_mode_show, external_mode_store);
  622. static void fsi_master_gpio_release(struct device *dev)
  623. {
  624. struct fsi_master_gpio *master = to_fsi_master_gpio(dev_to_fsi_master(dev));
  625. of_node_put(dev_of_node(master->dev));
  626. kfree(master);
  627. }
  628. static int fsi_master_gpio_probe(struct platform_device *pdev)
  629. {
  630. struct fsi_master_gpio *master;
  631. struct gpio_desc *gpio;
  632. int rc;
  633. master = kzalloc(sizeof(*master), GFP_KERNEL);
  634. if (!master)
  635. return -ENOMEM;
  636. master->dev = &pdev->dev;
  637. master->master.dev.parent = master->dev;
  638. master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
  639. master->master.dev.release = fsi_master_gpio_release;
  640. master->last_addr = LAST_ADDR_INVALID;
  641. gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
  642. if (IS_ERR(gpio)) {
  643. dev_err(&pdev->dev, "failed to get clock gpio\n");
  644. rc = PTR_ERR(gpio);
  645. goto err_free;
  646. }
  647. master->gpio_clk = gpio;
  648. gpio = devm_gpiod_get(&pdev->dev, "data", 0);
  649. if (IS_ERR(gpio)) {
  650. dev_err(&pdev->dev, "failed to get data gpio\n");
  651. rc = PTR_ERR(gpio);
  652. goto err_free;
  653. }
  654. master->gpio_data = gpio;
  655. /* Optional GPIOs */
  656. gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
  657. if (IS_ERR(gpio)) {
  658. dev_err(&pdev->dev, "failed to get trans gpio\n");
  659. rc = PTR_ERR(gpio);
  660. goto err_free;
  661. }
  662. master->gpio_trans = gpio;
  663. gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
  664. if (IS_ERR(gpio)) {
  665. dev_err(&pdev->dev, "failed to get enable gpio\n");
  666. rc = PTR_ERR(gpio);
  667. goto err_free;
  668. }
  669. master->gpio_enable = gpio;
  670. gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
  671. if (IS_ERR(gpio)) {
  672. dev_err(&pdev->dev, "failed to get mux gpio\n");
  673. rc = PTR_ERR(gpio);
  674. goto err_free;
  675. }
  676. master->gpio_mux = gpio;
  677. /*
  678. * Check if GPIO block is slow enought that no extra delays
  679. * are necessary. This improves performance on ast2500 by
  680. * an order of magnitude.
  681. */
  682. master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
  683. /* Default FSI command delays */
  684. master->t_send_delay = FSI_SEND_DELAY_CLOCKS;
  685. master->t_echo_delay = FSI_ECHO_DELAY_CLOCKS;
  686. master->master.n_links = 1;
  687. master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
  688. master->master.read = fsi_master_gpio_read;
  689. master->master.write = fsi_master_gpio_write;
  690. master->master.term = fsi_master_gpio_term;
  691. master->master.send_break = fsi_master_gpio_break;
  692. master->master.link_enable = fsi_master_gpio_link_enable;
  693. master->master.link_config = fsi_master_gpio_link_config;
  694. platform_set_drvdata(pdev, master);
  695. mutex_init(&master->cmd_lock);
  696. fsi_master_gpio_init(master);
  697. rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
  698. if (rc)
  699. goto err_free;
  700. rc = fsi_master_register(&master->master);
  701. if (rc) {
  702. device_remove_file(&pdev->dev, &dev_attr_external_mode);
  703. put_device(&master->master.dev);
  704. return rc;
  705. }
  706. return 0;
  707. err_free:
  708. kfree(master);
  709. return rc;
  710. }
  711. static int fsi_master_gpio_remove(struct platform_device *pdev)
  712. {
  713. struct fsi_master_gpio *master = platform_get_drvdata(pdev);
  714. device_remove_file(&pdev->dev, &dev_attr_external_mode);
  715. fsi_master_unregister(&master->master);
  716. return 0;
  717. }
  718. static const struct of_device_id fsi_master_gpio_match[] = {
  719. { .compatible = "fsi-master-gpio" },
  720. { },
  721. };
  722. static struct platform_driver fsi_master_gpio_driver = {
  723. .driver = {
  724. .name = "fsi-master-gpio",
  725. .of_match_table = fsi_master_gpio_match,
  726. },
  727. .probe = fsi_master_gpio_probe,
  728. .remove = fsi_master_gpio_remove,
  729. };
  730. module_platform_driver(fsi_master_gpio_driver);
  731. MODULE_LICENSE("GPL");