fsi-master-gpio.c 21 KB

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