regmap-spi-avmm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register map access API - SPI AVMM support
  4. //
  5. // Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
  6. #include <linux/module.h>
  7. #include <linux/regmap.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/swab.h>
  10. /*
  11. * This driver implements the regmap operations for a generic SPI
  12. * master to access the registers of the spi slave chip which has an
  13. * Avalone bus in it.
  14. *
  15. * The "SPI slave to Avalon Master Bridge" (spi-avmm) IP should be integrated
  16. * in the spi slave chip. The IP acts as a bridge to convert encoded streams of
  17. * bytes from the host to the internal register read/write on Avalon bus. In
  18. * order to issue register access requests to the slave chip, the host should
  19. * send formatted bytes that conform to the transfer protocol.
  20. * The transfer protocol contains 3 layers: transaction layer, packet layer
  21. * and physical layer.
  22. *
  23. * Reference Documents could be found at:
  24. * https://www.intel.com/content/www/us/en/programmable/documentation/sfo1400787952932.html
  25. *
  26. * Chapter "SPI Slave/JTAG to Avalon Master Bridge Cores" is a general
  27. * introduction to the protocol.
  28. *
  29. * Chapter "Avalon Packets to Transactions Converter Core" describes
  30. * the transaction layer.
  31. *
  32. * Chapter "Avalon-ST Bytes to Packets and Packets to Bytes Converter Cores"
  33. * describes the packet layer.
  34. *
  35. * Chapter "Avalon-ST Serial Peripheral Interface Core" describes the
  36. * physical layer.
  37. *
  38. *
  39. * When host issues a regmap read/write, the driver will transform the request
  40. * to byte stream layer by layer. It formats the register addr, value and
  41. * length to the transaction layer request, then converts the request to packet
  42. * layer bytes stream and then to physical layer bytes stream. Finally the
  43. * driver sends the formatted byte stream over SPI bus to the slave chip.
  44. *
  45. * The spi-avmm IP on the slave chip decodes the byte stream and initiates
  46. * register read/write on its internal Avalon bus, and then encodes the
  47. * response to byte stream and sends back to host.
  48. *
  49. * The driver receives the byte stream, reverses the 3 layers transformation,
  50. * and finally gets the response value (read out data for register read,
  51. * successful written size for register write).
  52. */
  53. #define PKT_SOP 0x7a
  54. #define PKT_EOP 0x7b
  55. #define PKT_CHANNEL 0x7c
  56. #define PKT_ESC 0x7d
  57. #define PHY_IDLE 0x4a
  58. #define PHY_ESC 0x4d
  59. #define TRANS_CODE_WRITE 0x0
  60. #define TRANS_CODE_SEQ_WRITE 0x4
  61. #define TRANS_CODE_READ 0x10
  62. #define TRANS_CODE_SEQ_READ 0x14
  63. #define TRANS_CODE_NO_TRANS 0x7f
  64. #define SPI_AVMM_XFER_TIMEOUT (msecs_to_jiffies(200))
  65. /* slave's register addr is 32 bits */
  66. #define SPI_AVMM_REG_SIZE 4UL
  67. /* slave's register value is 32 bits */
  68. #define SPI_AVMM_VAL_SIZE 4UL
  69. /*
  70. * max rx size could be larger. But considering the buffer consuming,
  71. * it is proper that we limit 1KB xfer at max.
  72. */
  73. #define MAX_READ_CNT 256UL
  74. #define MAX_WRITE_CNT 1UL
  75. struct trans_req_header {
  76. u8 code;
  77. u8 rsvd;
  78. __be16 size;
  79. __be32 addr;
  80. } __packed;
  81. struct trans_resp_header {
  82. u8 r_code;
  83. u8 rsvd;
  84. __be16 size;
  85. } __packed;
  86. #define TRANS_REQ_HD_SIZE (sizeof(struct trans_req_header))
  87. #define TRANS_RESP_HD_SIZE (sizeof(struct trans_resp_header))
  88. /*
  89. * In transaction layer,
  90. * the write request format is: Transaction request header + data
  91. * the read request format is: Transaction request header
  92. * the write response format is: Transaction response header
  93. * the read response format is: pure data, no Transaction response header
  94. */
  95. #define TRANS_WR_TX_SIZE(n) (TRANS_REQ_HD_SIZE + SPI_AVMM_VAL_SIZE * (n))
  96. #define TRANS_RD_TX_SIZE TRANS_REQ_HD_SIZE
  97. #define TRANS_TX_MAX TRANS_WR_TX_SIZE(MAX_WRITE_CNT)
  98. #define TRANS_RD_RX_SIZE(n) (SPI_AVMM_VAL_SIZE * (n))
  99. #define TRANS_WR_RX_SIZE TRANS_RESP_HD_SIZE
  100. #define TRANS_RX_MAX TRANS_RD_RX_SIZE(MAX_READ_CNT)
  101. /* tx & rx share one transaction layer buffer */
  102. #define TRANS_BUF_SIZE ((TRANS_TX_MAX > TRANS_RX_MAX) ? \
  103. TRANS_TX_MAX : TRANS_RX_MAX)
  104. /*
  105. * In tx phase, the host prepares all the phy layer bytes of a request in the
  106. * phy buffer and sends them in a batch.
  107. *
  108. * The packet layer and physical layer defines several special chars for
  109. * various purpose, when a transaction layer byte hits one of these special
  110. * chars, it should be escaped. The escape rule is, "Escape char first,
  111. * following the byte XOR'ed with 0x20".
  112. *
  113. * This macro defines the max possible length of the phy data. In the worst
  114. * case, all transaction layer bytes need to be escaped (so the data length
  115. * doubles), plus 4 special chars (SOP, CHANNEL, CHANNEL_NUM, EOP). Finally
  116. * we should make sure the length is aligned to SPI BPW.
  117. */
  118. #define PHY_TX_MAX ALIGN(2 * TRANS_TX_MAX + 4, 4)
  119. /*
  120. * Unlike tx, phy rx is affected by possible PHY_IDLE bytes from slave, the max
  121. * length of the rx bit stream is unpredictable. So the driver reads the words
  122. * one by one, and parses each word immediately into transaction layer buffer.
  123. * Only one word length of phy buffer is used for rx.
  124. */
  125. #define PHY_BUF_SIZE PHY_TX_MAX
  126. /**
  127. * struct spi_avmm_bridge - SPI slave to AVMM bus master bridge
  128. *
  129. * @spi: spi slave associated with this bridge.
  130. * @word_len: bytes of word for spi transfer.
  131. * @trans_len: length of valid data in trans_buf.
  132. * @phy_len: length of valid data in phy_buf.
  133. * @trans_buf: the bridge buffer for transaction layer data.
  134. * @phy_buf: the bridge buffer for physical layer data.
  135. * @swap_words: the word swapping cb for phy data. NULL if not needed.
  136. *
  137. * As a device's registers are implemented on the AVMM bus address space, it
  138. * requires the driver to issue formatted requests to spi slave to AVMM bus
  139. * master bridge to perform register access.
  140. */
  141. struct spi_avmm_bridge {
  142. struct spi_device *spi;
  143. unsigned char word_len;
  144. unsigned int trans_len;
  145. unsigned int phy_len;
  146. /* bridge buffer used in translation between protocol layers */
  147. char trans_buf[TRANS_BUF_SIZE];
  148. char phy_buf[PHY_BUF_SIZE];
  149. void (*swap_words)(void *buf, unsigned int len);
  150. };
  151. static void br_swap_words_32(void *buf, unsigned int len)
  152. {
  153. swab32_array(buf, len / 4);
  154. }
  155. /*
  156. * Format transaction layer data in br->trans_buf according to the register
  157. * access request, Store valid transaction layer data length in br->trans_len.
  158. */
  159. static int br_trans_tx_prepare(struct spi_avmm_bridge *br, bool is_read, u32 reg,
  160. u32 *wr_val, u32 count)
  161. {
  162. struct trans_req_header *header;
  163. unsigned int trans_len;
  164. u8 code;
  165. __le32 *data;
  166. int i;
  167. if (is_read) {
  168. if (count == 1)
  169. code = TRANS_CODE_READ;
  170. else
  171. code = TRANS_CODE_SEQ_READ;
  172. } else {
  173. if (count == 1)
  174. code = TRANS_CODE_WRITE;
  175. else
  176. code = TRANS_CODE_SEQ_WRITE;
  177. }
  178. header = (struct trans_req_header *)br->trans_buf;
  179. header->code = code;
  180. header->rsvd = 0;
  181. header->size = cpu_to_be16((u16)count * SPI_AVMM_VAL_SIZE);
  182. header->addr = cpu_to_be32(reg);
  183. trans_len = TRANS_REQ_HD_SIZE;
  184. if (!is_read) {
  185. trans_len += SPI_AVMM_VAL_SIZE * count;
  186. if (trans_len > sizeof(br->trans_buf))
  187. return -ENOMEM;
  188. data = (__le32 *)(br->trans_buf + TRANS_REQ_HD_SIZE);
  189. for (i = 0; i < count; i++)
  190. *data++ = cpu_to_le32(*wr_val++);
  191. }
  192. /* Store valid trans data length for next layer */
  193. br->trans_len = trans_len;
  194. return 0;
  195. }
  196. /*
  197. * Convert transaction layer data (in br->trans_buf) to phy layer data, store
  198. * them in br->phy_buf. Pad the phy_buf aligned with SPI's BPW. Store valid phy
  199. * layer data length in br->phy_len.
  200. *
  201. * phy_buf len should be aligned with SPI's BPW. Spare bytes should be padded
  202. * with PHY_IDLE, then the slave will just drop them.
  203. *
  204. * The driver will not simply pad 4a at the tail. The concern is that driver
  205. * will not store MISO data during tx phase, if the driver pads 4a at the tail,
  206. * it is possible that if the slave is fast enough to response at the padding
  207. * time. As a result these rx bytes are lost. In the following case, 7a,7c,00
  208. * will lost.
  209. * MOSI ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|4a|4a|4a| |XX|XX|...
  210. * MISO ...|4a|4a|4a|4a| |4a|4a|4a|4a| |4a|4a|4a|4a| |4a|7a|7c|00| |78|56|...
  211. *
  212. * So the driver moves EOP and bytes after EOP to the end of the aligned size,
  213. * then fill the hole with PHY_IDLE. As following:
  214. * before pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|7b| |40|
  215. * after pad ...|7a|7c|00|10| |00|00|04|02| |4b|7d|5a|4a| |4a|4a|7b|40|
  216. * Then if the slave will not get the entire packet before the tx phase is
  217. * over, it can't responsed to anything either.
  218. */
  219. static int br_pkt_phy_tx_prepare(struct spi_avmm_bridge *br)
  220. {
  221. char *tb, *tb_end, *pb, *pb_limit, *pb_eop = NULL;
  222. unsigned int aligned_phy_len, move_size;
  223. bool need_esc = false;
  224. tb = br->trans_buf;
  225. tb_end = tb + br->trans_len;
  226. pb = br->phy_buf;
  227. pb_limit = pb + ARRAY_SIZE(br->phy_buf);
  228. *pb++ = PKT_SOP;
  229. /*
  230. * The driver doesn't support multiple channels so the channel number
  231. * is always 0.
  232. */
  233. *pb++ = PKT_CHANNEL;
  234. *pb++ = 0x0;
  235. for (; pb < pb_limit && tb < tb_end; pb++) {
  236. if (need_esc) {
  237. *pb = *tb++ ^ 0x20;
  238. need_esc = false;
  239. continue;
  240. }
  241. /* EOP should be inserted before the last valid char */
  242. if (tb == tb_end - 1 && !pb_eop) {
  243. *pb = PKT_EOP;
  244. pb_eop = pb;
  245. continue;
  246. }
  247. /*
  248. * insert an ESCAPE char if the data value equals any special
  249. * char.
  250. */
  251. switch (*tb) {
  252. case PKT_SOP:
  253. case PKT_EOP:
  254. case PKT_CHANNEL:
  255. case PKT_ESC:
  256. *pb = PKT_ESC;
  257. need_esc = true;
  258. break;
  259. case PHY_IDLE:
  260. case PHY_ESC:
  261. *pb = PHY_ESC;
  262. need_esc = true;
  263. break;
  264. default:
  265. *pb = *tb++;
  266. break;
  267. }
  268. }
  269. /* The phy buffer is used out but transaction layer data remains */
  270. if (tb < tb_end)
  271. return -ENOMEM;
  272. /* Store valid phy data length for spi transfer */
  273. br->phy_len = pb - br->phy_buf;
  274. if (br->word_len == 1)
  275. return 0;
  276. /* Do phy buf padding if word_len > 1 byte. */
  277. aligned_phy_len = ALIGN(br->phy_len, br->word_len);
  278. if (aligned_phy_len > sizeof(br->phy_buf))
  279. return -ENOMEM;
  280. if (aligned_phy_len == br->phy_len)
  281. return 0;
  282. /* move EOP and bytes after EOP to the end of aligned size */
  283. move_size = pb - pb_eop;
  284. memmove(&br->phy_buf[aligned_phy_len - move_size], pb_eop, move_size);
  285. /* fill the hole with PHY_IDLEs */
  286. memset(pb_eop, PHY_IDLE, aligned_phy_len - br->phy_len);
  287. /* update the phy data length */
  288. br->phy_len = aligned_phy_len;
  289. return 0;
  290. }
  291. /*
  292. * In tx phase, the slave only returns PHY_IDLE (0x4a). So the driver will
  293. * ignore rx in tx phase.
  294. */
  295. static int br_do_tx(struct spi_avmm_bridge *br)
  296. {
  297. /* reorder words for spi transfer */
  298. if (br->swap_words)
  299. br->swap_words(br->phy_buf, br->phy_len);
  300. /* send all data in phy_buf */
  301. return spi_write(br->spi, br->phy_buf, br->phy_len);
  302. }
  303. /*
  304. * This function read the rx byte stream from SPI word by word and convert
  305. * them to transaction layer data in br->trans_buf. It also stores the length
  306. * of rx transaction layer data in br->trans_len
  307. *
  308. * The slave may send an unknown number of PHY_IDLEs in rx phase, so we cannot
  309. * prepare a fixed length buffer to receive all of the rx data in a batch. We
  310. * have to read word by word and convert them to transaction layer data at
  311. * once.
  312. */
  313. static int br_do_rx_and_pkt_phy_parse(struct spi_avmm_bridge *br)
  314. {
  315. bool eop_found = false, channel_found = false, esc_found = false;
  316. bool valid_word = false, last_try = false;
  317. struct device *dev = &br->spi->dev;
  318. char *pb, *tb_limit, *tb = NULL;
  319. unsigned long poll_timeout;
  320. int ret, i;
  321. tb_limit = br->trans_buf + ARRAY_SIZE(br->trans_buf);
  322. pb = br->phy_buf;
  323. poll_timeout = jiffies + SPI_AVMM_XFER_TIMEOUT;
  324. while (tb < tb_limit) {
  325. ret = spi_read(br->spi, pb, br->word_len);
  326. if (ret)
  327. return ret;
  328. /* reorder the word back */
  329. if (br->swap_words)
  330. br->swap_words(pb, br->word_len);
  331. valid_word = false;
  332. for (i = 0; i < br->word_len; i++) {
  333. /* drop everything before first SOP */
  334. if (!tb && pb[i] != PKT_SOP)
  335. continue;
  336. /* drop PHY_IDLE */
  337. if (pb[i] == PHY_IDLE)
  338. continue;
  339. valid_word = true;
  340. /*
  341. * We don't support multiple channels, so error out if
  342. * a non-zero channel number is found.
  343. */
  344. if (channel_found) {
  345. if (pb[i] != 0) {
  346. dev_err(dev, "%s channel num != 0\n",
  347. __func__);
  348. return -EFAULT;
  349. }
  350. channel_found = false;
  351. continue;
  352. }
  353. switch (pb[i]) {
  354. case PKT_SOP:
  355. /*
  356. * reset the parsing if a second SOP appears.
  357. */
  358. tb = br->trans_buf;
  359. eop_found = false;
  360. channel_found = false;
  361. esc_found = false;
  362. break;
  363. case PKT_EOP:
  364. /*
  365. * No special char is expected after ESC char.
  366. * No special char (except ESC & PHY_IDLE) is
  367. * expected after EOP char.
  368. *
  369. * The special chars are all dropped.
  370. */
  371. if (esc_found || eop_found)
  372. return -EFAULT;
  373. eop_found = true;
  374. break;
  375. case PKT_CHANNEL:
  376. if (esc_found || eop_found)
  377. return -EFAULT;
  378. channel_found = true;
  379. break;
  380. case PKT_ESC:
  381. case PHY_ESC:
  382. if (esc_found)
  383. return -EFAULT;
  384. esc_found = true;
  385. break;
  386. default:
  387. /* Record the normal byte in trans_buf. */
  388. if (esc_found) {
  389. *tb++ = pb[i] ^ 0x20;
  390. esc_found = false;
  391. } else {
  392. *tb++ = pb[i];
  393. }
  394. /*
  395. * We get the last normal byte after EOP, it is
  396. * time we finish. Normally the function should
  397. * return here.
  398. */
  399. if (eop_found) {
  400. br->trans_len = tb - br->trans_buf;
  401. return 0;
  402. }
  403. }
  404. }
  405. if (valid_word) {
  406. /* update poll timeout when we get valid word */
  407. poll_timeout = jiffies + SPI_AVMM_XFER_TIMEOUT;
  408. last_try = false;
  409. } else {
  410. /*
  411. * We timeout when rx keeps invalid for some time. But
  412. * it is possible we are scheduled out for long time
  413. * after a spi_read. So when we are scheduled in, a SW
  414. * timeout happens. But actually HW may have worked fine and
  415. * has been ready long time ago. So we need to do an extra
  416. * read, if we get a valid word then we could continue rx,
  417. * otherwise real a HW issue happens.
  418. */
  419. if (last_try)
  420. return -ETIMEDOUT;
  421. if (time_after(jiffies, poll_timeout))
  422. last_try = true;
  423. }
  424. }
  425. /*
  426. * We have used out all transfer layer buffer but cannot find the end
  427. * of the byte stream.
  428. */
  429. dev_err(dev, "%s transfer buffer is full but rx doesn't end\n",
  430. __func__);
  431. return -EFAULT;
  432. }
  433. /*
  434. * For read transactions, the avmm bus will directly return register values
  435. * without transaction response header.
  436. */
  437. static int br_rd_trans_rx_parse(struct spi_avmm_bridge *br,
  438. u32 *val, unsigned int expected_count)
  439. {
  440. unsigned int i, trans_len = br->trans_len;
  441. __le32 *data;
  442. if (expected_count * SPI_AVMM_VAL_SIZE != trans_len)
  443. return -EFAULT;
  444. data = (__le32 *)br->trans_buf;
  445. for (i = 0; i < expected_count; i++)
  446. *val++ = le32_to_cpu(*data++);
  447. return 0;
  448. }
  449. /*
  450. * For write transactions, the slave will return a transaction response
  451. * header.
  452. */
  453. static int br_wr_trans_rx_parse(struct spi_avmm_bridge *br,
  454. unsigned int expected_count)
  455. {
  456. unsigned int trans_len = br->trans_len;
  457. struct trans_resp_header *resp;
  458. u8 code;
  459. u16 val_len;
  460. if (trans_len != TRANS_RESP_HD_SIZE)
  461. return -EFAULT;
  462. resp = (struct trans_resp_header *)br->trans_buf;
  463. code = resp->r_code ^ 0x80;
  464. val_len = be16_to_cpu(resp->size);
  465. if (!val_len || val_len != expected_count * SPI_AVMM_VAL_SIZE)
  466. return -EFAULT;
  467. /* error out if the trans code doesn't align with the val size */
  468. if ((val_len == SPI_AVMM_VAL_SIZE && code != TRANS_CODE_WRITE) ||
  469. (val_len > SPI_AVMM_VAL_SIZE && code != TRANS_CODE_SEQ_WRITE))
  470. return -EFAULT;
  471. return 0;
  472. }
  473. static int do_reg_access(void *context, bool is_read, unsigned int reg,
  474. unsigned int *value, unsigned int count)
  475. {
  476. struct spi_avmm_bridge *br = context;
  477. int ret;
  478. /* invalidate bridge buffers first */
  479. br->trans_len = 0;
  480. br->phy_len = 0;
  481. ret = br_trans_tx_prepare(br, is_read, reg, value, count);
  482. if (ret)
  483. return ret;
  484. ret = br_pkt_phy_tx_prepare(br);
  485. if (ret)
  486. return ret;
  487. ret = br_do_tx(br);
  488. if (ret)
  489. return ret;
  490. ret = br_do_rx_and_pkt_phy_parse(br);
  491. if (ret)
  492. return ret;
  493. if (is_read)
  494. return br_rd_trans_rx_parse(br, value, count);
  495. else
  496. return br_wr_trans_rx_parse(br, count);
  497. }
  498. static int regmap_spi_avmm_gather_write(void *context,
  499. const void *reg_buf, size_t reg_len,
  500. const void *val_buf, size_t val_len)
  501. {
  502. if (reg_len != SPI_AVMM_REG_SIZE)
  503. return -EINVAL;
  504. if (!IS_ALIGNED(val_len, SPI_AVMM_VAL_SIZE))
  505. return -EINVAL;
  506. return do_reg_access(context, false, *(u32 *)reg_buf, (u32 *)val_buf,
  507. val_len / SPI_AVMM_VAL_SIZE);
  508. }
  509. static int regmap_spi_avmm_write(void *context, const void *data, size_t bytes)
  510. {
  511. if (bytes < SPI_AVMM_REG_SIZE + SPI_AVMM_VAL_SIZE)
  512. return -EINVAL;
  513. return regmap_spi_avmm_gather_write(context, data, SPI_AVMM_REG_SIZE,
  514. data + SPI_AVMM_REG_SIZE,
  515. bytes - SPI_AVMM_REG_SIZE);
  516. }
  517. static int regmap_spi_avmm_read(void *context,
  518. const void *reg_buf, size_t reg_len,
  519. void *val_buf, size_t val_len)
  520. {
  521. if (reg_len != SPI_AVMM_REG_SIZE)
  522. return -EINVAL;
  523. if (!IS_ALIGNED(val_len, SPI_AVMM_VAL_SIZE))
  524. return -EINVAL;
  525. return do_reg_access(context, true, *(u32 *)reg_buf, val_buf,
  526. (val_len / SPI_AVMM_VAL_SIZE));
  527. }
  528. static struct spi_avmm_bridge *
  529. spi_avmm_bridge_ctx_gen(struct spi_device *spi)
  530. {
  531. struct spi_avmm_bridge *br;
  532. if (!spi)
  533. return ERR_PTR(-ENODEV);
  534. /* Only support BPW == 8 or 32 now. Try 32 BPW first. */
  535. spi->mode = SPI_MODE_1;
  536. spi->bits_per_word = 32;
  537. if (spi_setup(spi)) {
  538. spi->bits_per_word = 8;
  539. if (spi_setup(spi))
  540. return ERR_PTR(-EINVAL);
  541. }
  542. br = kzalloc(sizeof(*br), GFP_KERNEL);
  543. if (!br)
  544. return ERR_PTR(-ENOMEM);
  545. br->spi = spi;
  546. br->word_len = spi->bits_per_word / 8;
  547. if (br->word_len == 4) {
  548. /*
  549. * The protocol requires little endian byte order but MSB
  550. * first. So driver needs to swap the byte order word by word
  551. * if word length > 1.
  552. */
  553. br->swap_words = br_swap_words_32;
  554. }
  555. return br;
  556. }
  557. static void spi_avmm_bridge_ctx_free(void *context)
  558. {
  559. kfree(context);
  560. }
  561. static const struct regmap_bus regmap_spi_avmm_bus = {
  562. .write = regmap_spi_avmm_write,
  563. .gather_write = regmap_spi_avmm_gather_write,
  564. .read = regmap_spi_avmm_read,
  565. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  566. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  567. .max_raw_read = SPI_AVMM_VAL_SIZE * MAX_READ_CNT,
  568. .max_raw_write = SPI_AVMM_VAL_SIZE * MAX_WRITE_CNT,
  569. .free_context = spi_avmm_bridge_ctx_free,
  570. };
  571. struct regmap *__regmap_init_spi_avmm(struct spi_device *spi,
  572. const struct regmap_config *config,
  573. struct lock_class_key *lock_key,
  574. const char *lock_name)
  575. {
  576. struct spi_avmm_bridge *bridge;
  577. struct regmap *map;
  578. bridge = spi_avmm_bridge_ctx_gen(spi);
  579. if (IS_ERR(bridge))
  580. return ERR_CAST(bridge);
  581. map = __regmap_init(&spi->dev, &regmap_spi_avmm_bus,
  582. bridge, config, lock_key, lock_name);
  583. if (IS_ERR(map)) {
  584. spi_avmm_bridge_ctx_free(bridge);
  585. return ERR_CAST(map);
  586. }
  587. return map;
  588. }
  589. EXPORT_SYMBOL_GPL(__regmap_init_spi_avmm);
  590. struct regmap *__devm_regmap_init_spi_avmm(struct spi_device *spi,
  591. const struct regmap_config *config,
  592. struct lock_class_key *lock_key,
  593. const char *lock_name)
  594. {
  595. struct spi_avmm_bridge *bridge;
  596. struct regmap *map;
  597. bridge = spi_avmm_bridge_ctx_gen(spi);
  598. if (IS_ERR(bridge))
  599. return ERR_CAST(bridge);
  600. map = __devm_regmap_init(&spi->dev, &regmap_spi_avmm_bus,
  601. bridge, config, lock_key, lock_name);
  602. if (IS_ERR(map)) {
  603. spi_avmm_bridge_ctx_free(bridge);
  604. return ERR_CAST(map);
  605. }
  606. return map;
  607. }
  608. EXPORT_SYMBOL_GPL(__devm_regmap_init_spi_avmm);
  609. MODULE_LICENSE("GPL v2");