spi-dln2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * Driver for the Diolan DLN-2 USB-SPI adapter
  3. *
  4. * Copyright (c) 2014 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/mfd/dln2.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/pm_runtime.h>
  16. #include <asm/unaligned.h>
  17. #define DLN2_SPI_MODULE_ID 0x02
  18. #define DLN2_SPI_CMD(cmd) DLN2_CMD(cmd, DLN2_SPI_MODULE_ID)
  19. /* SPI commands */
  20. #define DLN2_SPI_GET_PORT_COUNT DLN2_SPI_CMD(0x00)
  21. #define DLN2_SPI_ENABLE DLN2_SPI_CMD(0x11)
  22. #define DLN2_SPI_DISABLE DLN2_SPI_CMD(0x12)
  23. #define DLN2_SPI_IS_ENABLED DLN2_SPI_CMD(0x13)
  24. #define DLN2_SPI_SET_MODE DLN2_SPI_CMD(0x14)
  25. #define DLN2_SPI_GET_MODE DLN2_SPI_CMD(0x15)
  26. #define DLN2_SPI_SET_FRAME_SIZE DLN2_SPI_CMD(0x16)
  27. #define DLN2_SPI_GET_FRAME_SIZE DLN2_SPI_CMD(0x17)
  28. #define DLN2_SPI_SET_FREQUENCY DLN2_SPI_CMD(0x18)
  29. #define DLN2_SPI_GET_FREQUENCY DLN2_SPI_CMD(0x19)
  30. #define DLN2_SPI_READ_WRITE DLN2_SPI_CMD(0x1A)
  31. #define DLN2_SPI_READ DLN2_SPI_CMD(0x1B)
  32. #define DLN2_SPI_WRITE DLN2_SPI_CMD(0x1C)
  33. #define DLN2_SPI_SET_DELAY_BETWEEN_SS DLN2_SPI_CMD(0x20)
  34. #define DLN2_SPI_GET_DELAY_BETWEEN_SS DLN2_SPI_CMD(0x21)
  35. #define DLN2_SPI_SET_DELAY_AFTER_SS DLN2_SPI_CMD(0x22)
  36. #define DLN2_SPI_GET_DELAY_AFTER_SS DLN2_SPI_CMD(0x23)
  37. #define DLN2_SPI_SET_DELAY_BETWEEN_FRAMES DLN2_SPI_CMD(0x24)
  38. #define DLN2_SPI_GET_DELAY_BETWEEN_FRAMES DLN2_SPI_CMD(0x25)
  39. #define DLN2_SPI_SET_SS DLN2_SPI_CMD(0x26)
  40. #define DLN2_SPI_GET_SS DLN2_SPI_CMD(0x27)
  41. #define DLN2_SPI_RELEASE_SS DLN2_SPI_CMD(0x28)
  42. #define DLN2_SPI_SS_VARIABLE_ENABLE DLN2_SPI_CMD(0x2B)
  43. #define DLN2_SPI_SS_VARIABLE_DISABLE DLN2_SPI_CMD(0x2C)
  44. #define DLN2_SPI_SS_VARIABLE_IS_ENABLED DLN2_SPI_CMD(0x2D)
  45. #define DLN2_SPI_SS_AAT_ENABLE DLN2_SPI_CMD(0x2E)
  46. #define DLN2_SPI_SS_AAT_DISABLE DLN2_SPI_CMD(0x2F)
  47. #define DLN2_SPI_SS_AAT_IS_ENABLED DLN2_SPI_CMD(0x30)
  48. #define DLN2_SPI_SS_BETWEEN_FRAMES_ENABLE DLN2_SPI_CMD(0x31)
  49. #define DLN2_SPI_SS_BETWEEN_FRAMES_DISABLE DLN2_SPI_CMD(0x32)
  50. #define DLN2_SPI_SS_BETWEEN_FRAMES_IS_ENABLED DLN2_SPI_CMD(0x33)
  51. #define DLN2_SPI_SET_CPHA DLN2_SPI_CMD(0x34)
  52. #define DLN2_SPI_GET_CPHA DLN2_SPI_CMD(0x35)
  53. #define DLN2_SPI_SET_CPOL DLN2_SPI_CMD(0x36)
  54. #define DLN2_SPI_GET_CPOL DLN2_SPI_CMD(0x37)
  55. #define DLN2_SPI_SS_MULTI_ENABLE DLN2_SPI_CMD(0x38)
  56. #define DLN2_SPI_SS_MULTI_DISABLE DLN2_SPI_CMD(0x39)
  57. #define DLN2_SPI_SS_MULTI_IS_ENABLED DLN2_SPI_CMD(0x3A)
  58. #define DLN2_SPI_GET_SUPPORTED_MODES DLN2_SPI_CMD(0x40)
  59. #define DLN2_SPI_GET_SUPPORTED_CPHA_VALUES DLN2_SPI_CMD(0x41)
  60. #define DLN2_SPI_GET_SUPPORTED_CPOL_VALUES DLN2_SPI_CMD(0x42)
  61. #define DLN2_SPI_GET_SUPPORTED_FRAME_SIZES DLN2_SPI_CMD(0x43)
  62. #define DLN2_SPI_GET_SS_COUNT DLN2_SPI_CMD(0x44)
  63. #define DLN2_SPI_GET_MIN_FREQUENCY DLN2_SPI_CMD(0x45)
  64. #define DLN2_SPI_GET_MAX_FREQUENCY DLN2_SPI_CMD(0x46)
  65. #define DLN2_SPI_GET_MIN_DELAY_BETWEEN_SS DLN2_SPI_CMD(0x47)
  66. #define DLN2_SPI_GET_MAX_DELAY_BETWEEN_SS DLN2_SPI_CMD(0x48)
  67. #define DLN2_SPI_GET_MIN_DELAY_AFTER_SS DLN2_SPI_CMD(0x49)
  68. #define DLN2_SPI_GET_MAX_DELAY_AFTER_SS DLN2_SPI_CMD(0x4A)
  69. #define DLN2_SPI_GET_MIN_DELAY_BETWEEN_FRAMES DLN2_SPI_CMD(0x4B)
  70. #define DLN2_SPI_GET_MAX_DELAY_BETWEEN_FRAMES DLN2_SPI_CMD(0x4C)
  71. #define DLN2_SPI_MAX_XFER_SIZE 256
  72. #define DLN2_SPI_BUF_SIZE (DLN2_SPI_MAX_XFER_SIZE + 16)
  73. #define DLN2_SPI_ATTR_LEAVE_SS_LOW BIT(0)
  74. #define DLN2_TRANSFERS_WAIT_COMPLETE 1
  75. #define DLN2_TRANSFERS_CANCEL 0
  76. #define DLN2_RPM_AUTOSUSPEND_TIMEOUT 2000
  77. struct dln2_spi {
  78. struct platform_device *pdev;
  79. struct spi_master *master;
  80. u8 port;
  81. /*
  82. * This buffer will be used mainly for read/write operations. Since
  83. * they're quite large, we cannot use the stack. Protection is not
  84. * needed because all SPI communication is serialized by the SPI core.
  85. */
  86. void *buf;
  87. u8 bpw;
  88. u32 speed;
  89. u16 mode;
  90. u8 cs;
  91. };
  92. /*
  93. * Enable/Disable SPI module. The disable command will wait for transfers to
  94. * complete first.
  95. */
  96. static int dln2_spi_enable(struct dln2_spi *dln2, bool enable)
  97. {
  98. u16 cmd;
  99. struct {
  100. u8 port;
  101. u8 wait_for_completion;
  102. } tx;
  103. unsigned len = sizeof(tx);
  104. tx.port = dln2->port;
  105. if (enable) {
  106. cmd = DLN2_SPI_ENABLE;
  107. len -= sizeof(tx.wait_for_completion);
  108. } else {
  109. tx.wait_for_completion = DLN2_TRANSFERS_WAIT_COMPLETE;
  110. cmd = DLN2_SPI_DISABLE;
  111. }
  112. return dln2_transfer_tx(dln2->pdev, cmd, &tx, len);
  113. }
  114. /*
  115. * Select/unselect multiple CS lines. The selected lines will be automatically
  116. * toggled LOW/HIGH by the board firmware during transfers, provided they're
  117. * enabled first.
  118. *
  119. * Ex: cs_mask = 0x03 -> CS0 & CS1 will be selected and the next WR/RD operation
  120. * will toggle the lines LOW/HIGH automatically.
  121. */
  122. static int dln2_spi_cs_set(struct dln2_spi *dln2, u8 cs_mask)
  123. {
  124. struct {
  125. u8 port;
  126. u8 cs;
  127. } tx;
  128. tx.port = dln2->port;
  129. /*
  130. * According to Diolan docs, "a slave device can be selected by changing
  131. * the corresponding bit value to 0". The rest must be set to 1. Hence
  132. * the bitwise NOT in front.
  133. */
  134. tx.cs = ~cs_mask;
  135. return dln2_transfer_tx(dln2->pdev, DLN2_SPI_SET_SS, &tx, sizeof(tx));
  136. }
  137. /*
  138. * Select one CS line. The other lines will be un-selected.
  139. */
  140. static int dln2_spi_cs_set_one(struct dln2_spi *dln2, u8 cs)
  141. {
  142. return dln2_spi_cs_set(dln2, BIT(cs));
  143. }
  144. /*
  145. * Enable/disable CS lines for usage. The module has to be disabled first.
  146. */
  147. static int dln2_spi_cs_enable(struct dln2_spi *dln2, u8 cs_mask, bool enable)
  148. {
  149. struct {
  150. u8 port;
  151. u8 cs;
  152. } tx;
  153. u16 cmd;
  154. tx.port = dln2->port;
  155. tx.cs = cs_mask;
  156. cmd = enable ? DLN2_SPI_SS_MULTI_ENABLE : DLN2_SPI_SS_MULTI_DISABLE;
  157. return dln2_transfer_tx(dln2->pdev, cmd, &tx, sizeof(tx));
  158. }
  159. static int dln2_spi_cs_enable_all(struct dln2_spi *dln2, bool enable)
  160. {
  161. u8 cs_mask = GENMASK(dln2->master->num_chipselect - 1, 0);
  162. return dln2_spi_cs_enable(dln2, cs_mask, enable);
  163. }
  164. static int dln2_spi_get_cs_num(struct dln2_spi *dln2, u16 *cs_num)
  165. {
  166. int ret;
  167. struct {
  168. u8 port;
  169. } tx;
  170. struct {
  171. __le16 cs_count;
  172. } rx;
  173. unsigned rx_len = sizeof(rx);
  174. tx.port = dln2->port;
  175. ret = dln2_transfer(dln2->pdev, DLN2_SPI_GET_SS_COUNT, &tx, sizeof(tx),
  176. &rx, &rx_len);
  177. if (ret < 0)
  178. return ret;
  179. if (rx_len < sizeof(rx))
  180. return -EPROTO;
  181. *cs_num = le16_to_cpu(rx.cs_count);
  182. dev_dbg(&dln2->pdev->dev, "cs_num = %d\n", *cs_num);
  183. return 0;
  184. }
  185. static int dln2_spi_get_speed(struct dln2_spi *dln2, u16 cmd, u32 *freq)
  186. {
  187. int ret;
  188. struct {
  189. u8 port;
  190. } tx;
  191. struct {
  192. __le32 speed;
  193. } rx;
  194. unsigned rx_len = sizeof(rx);
  195. tx.port = dln2->port;
  196. ret = dln2_transfer(dln2->pdev, cmd, &tx, sizeof(tx), &rx, &rx_len);
  197. if (ret < 0)
  198. return ret;
  199. if (rx_len < sizeof(rx))
  200. return -EPROTO;
  201. *freq = le32_to_cpu(rx.speed);
  202. return 0;
  203. }
  204. /*
  205. * Get bus min/max frequencies.
  206. */
  207. static int dln2_spi_get_speed_range(struct dln2_spi *dln2, u32 *fmin, u32 *fmax)
  208. {
  209. int ret;
  210. ret = dln2_spi_get_speed(dln2, DLN2_SPI_GET_MIN_FREQUENCY, fmin);
  211. if (ret < 0)
  212. return ret;
  213. ret = dln2_spi_get_speed(dln2, DLN2_SPI_GET_MAX_FREQUENCY, fmax);
  214. if (ret < 0)
  215. return ret;
  216. dev_dbg(&dln2->pdev->dev, "freq_min = %d, freq_max = %d\n",
  217. *fmin, *fmax);
  218. return 0;
  219. }
  220. /*
  221. * Set the bus speed. The module will automatically round down to the closest
  222. * available frequency and returns it. The module has to be disabled first.
  223. */
  224. static int dln2_spi_set_speed(struct dln2_spi *dln2, u32 speed)
  225. {
  226. int ret;
  227. struct {
  228. u8 port;
  229. __le32 speed;
  230. } __packed tx;
  231. struct {
  232. __le32 speed;
  233. } rx;
  234. int rx_len = sizeof(rx);
  235. tx.port = dln2->port;
  236. tx.speed = cpu_to_le32(speed);
  237. ret = dln2_transfer(dln2->pdev, DLN2_SPI_SET_FREQUENCY, &tx, sizeof(tx),
  238. &rx, &rx_len);
  239. if (ret < 0)
  240. return ret;
  241. if (rx_len < sizeof(rx))
  242. return -EPROTO;
  243. return 0;
  244. }
  245. /*
  246. * Change CPOL & CPHA. The module has to be disabled first.
  247. */
  248. static int dln2_spi_set_mode(struct dln2_spi *dln2, u8 mode)
  249. {
  250. struct {
  251. u8 port;
  252. u8 mode;
  253. } tx;
  254. tx.port = dln2->port;
  255. tx.mode = mode;
  256. return dln2_transfer_tx(dln2->pdev, DLN2_SPI_SET_MODE, &tx, sizeof(tx));
  257. }
  258. /*
  259. * Change frame size. The module has to be disabled first.
  260. */
  261. static int dln2_spi_set_bpw(struct dln2_spi *dln2, u8 bpw)
  262. {
  263. struct {
  264. u8 port;
  265. u8 bpw;
  266. } tx;
  267. tx.port = dln2->port;
  268. tx.bpw = bpw;
  269. return dln2_transfer_tx(dln2->pdev, DLN2_SPI_SET_FRAME_SIZE,
  270. &tx, sizeof(tx));
  271. }
  272. static int dln2_spi_get_supported_frame_sizes(struct dln2_spi *dln2,
  273. u32 *bpw_mask)
  274. {
  275. int ret;
  276. struct {
  277. u8 port;
  278. } tx;
  279. struct {
  280. u8 count;
  281. u8 frame_sizes[36];
  282. } *rx = dln2->buf;
  283. unsigned rx_len = sizeof(*rx);
  284. int i;
  285. tx.port = dln2->port;
  286. ret = dln2_transfer(dln2->pdev, DLN2_SPI_GET_SUPPORTED_FRAME_SIZES,
  287. &tx, sizeof(tx), rx, &rx_len);
  288. if (ret < 0)
  289. return ret;
  290. if (rx_len < sizeof(*rx))
  291. return -EPROTO;
  292. if (rx->count > ARRAY_SIZE(rx->frame_sizes))
  293. return -EPROTO;
  294. *bpw_mask = 0;
  295. for (i = 0; i < rx->count; i++)
  296. *bpw_mask |= BIT(rx->frame_sizes[i] - 1);
  297. dev_dbg(&dln2->pdev->dev, "bpw_mask = 0x%X\n", *bpw_mask);
  298. return 0;
  299. }
  300. /*
  301. * Copy the data to DLN2 buffer and change the byte order to LE, requested by
  302. * DLN2 module. SPI core makes sure that the data length is a multiple of word
  303. * size.
  304. */
  305. static int dln2_spi_copy_to_buf(u8 *dln2_buf, const u8 *src, u16 len, u8 bpw)
  306. {
  307. #ifdef __LITTLE_ENDIAN
  308. memcpy(dln2_buf, src, len);
  309. #else
  310. if (bpw <= 8) {
  311. memcpy(dln2_buf, src, len);
  312. } else if (bpw <= 16) {
  313. __le16 *d = (__le16 *)dln2_buf;
  314. u16 *s = (u16 *)src;
  315. len = len / 2;
  316. while (len--)
  317. *d++ = cpu_to_le16p(s++);
  318. } else {
  319. __le32 *d = (__le32 *)dln2_buf;
  320. u32 *s = (u32 *)src;
  321. len = len / 4;
  322. while (len--)
  323. *d++ = cpu_to_le32p(s++);
  324. }
  325. #endif
  326. return 0;
  327. }
  328. /*
  329. * Copy the data from DLN2 buffer and convert to CPU byte order since the DLN2
  330. * buffer is LE ordered. SPI core makes sure that the data length is a multiple
  331. * of word size. The RX dln2_buf is 2 byte aligned so, for BE, we have to make
  332. * sure we avoid unaligned accesses for 32 bit case.
  333. */
  334. static int dln2_spi_copy_from_buf(u8 *dest, const u8 *dln2_buf, u16 len, u8 bpw)
  335. {
  336. #ifdef __LITTLE_ENDIAN
  337. memcpy(dest, dln2_buf, len);
  338. #else
  339. if (bpw <= 8) {
  340. memcpy(dest, dln2_buf, len);
  341. } else if (bpw <= 16) {
  342. u16 *d = (u16 *)dest;
  343. __le16 *s = (__le16 *)dln2_buf;
  344. len = len / 2;
  345. while (len--)
  346. *d++ = le16_to_cpup(s++);
  347. } else {
  348. u32 *d = (u32 *)dest;
  349. __le32 *s = (__le32 *)dln2_buf;
  350. len = len / 4;
  351. while (len--)
  352. *d++ = get_unaligned_le32(s++);
  353. }
  354. #endif
  355. return 0;
  356. }
  357. /*
  358. * Perform one write operation.
  359. */
  360. static int dln2_spi_write_one(struct dln2_spi *dln2, const u8 *data,
  361. u16 data_len, u8 attr)
  362. {
  363. struct {
  364. u8 port;
  365. __le16 size;
  366. u8 attr;
  367. u8 buf[DLN2_SPI_MAX_XFER_SIZE];
  368. } __packed *tx = dln2->buf;
  369. unsigned tx_len;
  370. BUILD_BUG_ON(sizeof(*tx) > DLN2_SPI_BUF_SIZE);
  371. if (data_len > DLN2_SPI_MAX_XFER_SIZE)
  372. return -EINVAL;
  373. tx->port = dln2->port;
  374. tx->size = cpu_to_le16(data_len);
  375. tx->attr = attr;
  376. dln2_spi_copy_to_buf(tx->buf, data, data_len, dln2->bpw);
  377. tx_len = sizeof(*tx) + data_len - DLN2_SPI_MAX_XFER_SIZE;
  378. return dln2_transfer_tx(dln2->pdev, DLN2_SPI_WRITE, tx, tx_len);
  379. }
  380. /*
  381. * Perform one read operation.
  382. */
  383. static int dln2_spi_read_one(struct dln2_spi *dln2, u8 *data,
  384. u16 data_len, u8 attr)
  385. {
  386. int ret;
  387. struct {
  388. u8 port;
  389. __le16 size;
  390. u8 attr;
  391. } __packed tx;
  392. struct {
  393. __le16 size;
  394. u8 buf[DLN2_SPI_MAX_XFER_SIZE];
  395. } __packed *rx = dln2->buf;
  396. unsigned rx_len = sizeof(*rx);
  397. BUILD_BUG_ON(sizeof(*rx) > DLN2_SPI_BUF_SIZE);
  398. if (data_len > DLN2_SPI_MAX_XFER_SIZE)
  399. return -EINVAL;
  400. tx.port = dln2->port;
  401. tx.size = cpu_to_le16(data_len);
  402. tx.attr = attr;
  403. ret = dln2_transfer(dln2->pdev, DLN2_SPI_READ, &tx, sizeof(tx),
  404. rx, &rx_len);
  405. if (ret < 0)
  406. return ret;
  407. if (rx_len < sizeof(rx->size) + data_len)
  408. return -EPROTO;
  409. if (le16_to_cpu(rx->size) != data_len)
  410. return -EPROTO;
  411. dln2_spi_copy_from_buf(data, rx->buf, data_len, dln2->bpw);
  412. return 0;
  413. }
  414. /*
  415. * Perform one write & read operation.
  416. */
  417. static int dln2_spi_read_write_one(struct dln2_spi *dln2, const u8 *tx_data,
  418. u8 *rx_data, u16 data_len, u8 attr)
  419. {
  420. int ret;
  421. struct {
  422. u8 port;
  423. __le16 size;
  424. u8 attr;
  425. u8 buf[DLN2_SPI_MAX_XFER_SIZE];
  426. } __packed *tx;
  427. struct {
  428. __le16 size;
  429. u8 buf[DLN2_SPI_MAX_XFER_SIZE];
  430. } __packed *rx;
  431. unsigned tx_len, rx_len;
  432. BUILD_BUG_ON(sizeof(*tx) > DLN2_SPI_BUF_SIZE ||
  433. sizeof(*rx) > DLN2_SPI_BUF_SIZE);
  434. if (data_len > DLN2_SPI_MAX_XFER_SIZE)
  435. return -EINVAL;
  436. /*
  437. * Since this is a pseudo full-duplex communication, we're perfectly
  438. * safe to use the same buffer for both tx and rx. When DLN2 sends the
  439. * response back, with the rx data, we don't need the tx buffer anymore.
  440. */
  441. tx = dln2->buf;
  442. rx = dln2->buf;
  443. tx->port = dln2->port;
  444. tx->size = cpu_to_le16(data_len);
  445. tx->attr = attr;
  446. dln2_spi_copy_to_buf(tx->buf, tx_data, data_len, dln2->bpw);
  447. tx_len = sizeof(*tx) + data_len - DLN2_SPI_MAX_XFER_SIZE;
  448. rx_len = sizeof(*rx);
  449. ret = dln2_transfer(dln2->pdev, DLN2_SPI_READ_WRITE, tx, tx_len,
  450. rx, &rx_len);
  451. if (ret < 0)
  452. return ret;
  453. if (rx_len < sizeof(rx->size) + data_len)
  454. return -EPROTO;
  455. if (le16_to_cpu(rx->size) != data_len)
  456. return -EPROTO;
  457. dln2_spi_copy_from_buf(rx_data, rx->buf, data_len, dln2->bpw);
  458. return 0;
  459. }
  460. /*
  461. * Read/Write wrapper. It will automatically split an operation into multiple
  462. * single ones due to device buffer constraints.
  463. */
  464. static int dln2_spi_rdwr(struct dln2_spi *dln2, const u8 *tx_data,
  465. u8 *rx_data, u16 data_len, u8 attr) {
  466. int ret;
  467. u16 len;
  468. u8 temp_attr;
  469. u16 remaining = data_len;
  470. u16 offset;
  471. do {
  472. if (remaining > DLN2_SPI_MAX_XFER_SIZE) {
  473. len = DLN2_SPI_MAX_XFER_SIZE;
  474. temp_attr = DLN2_SPI_ATTR_LEAVE_SS_LOW;
  475. } else {
  476. len = remaining;
  477. temp_attr = attr;
  478. }
  479. offset = data_len - remaining;
  480. if (tx_data && rx_data) {
  481. ret = dln2_spi_read_write_one(dln2,
  482. tx_data + offset,
  483. rx_data + offset,
  484. len, temp_attr);
  485. } else if (tx_data) {
  486. ret = dln2_spi_write_one(dln2,
  487. tx_data + offset,
  488. len, temp_attr);
  489. } else if (rx_data) {
  490. ret = dln2_spi_read_one(dln2,
  491. rx_data + offset,
  492. len, temp_attr);
  493. } else {
  494. return -EINVAL;
  495. }
  496. if (ret < 0)
  497. return ret;
  498. remaining -= len;
  499. } while (remaining);
  500. return 0;
  501. }
  502. static int dln2_spi_prepare_message(struct spi_master *master,
  503. struct spi_message *message)
  504. {
  505. int ret;
  506. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  507. struct spi_device *spi = message->spi;
  508. if (dln2->cs != spi->chip_select) {
  509. ret = dln2_spi_cs_set_one(dln2, spi->chip_select);
  510. if (ret < 0)
  511. return ret;
  512. dln2->cs = spi->chip_select;
  513. }
  514. return 0;
  515. }
  516. static int dln2_spi_transfer_setup(struct dln2_spi *dln2, u32 speed,
  517. u8 bpw, u8 mode)
  518. {
  519. int ret;
  520. bool bus_setup_change;
  521. bus_setup_change = dln2->speed != speed || dln2->mode != mode ||
  522. dln2->bpw != bpw;
  523. if (!bus_setup_change)
  524. return 0;
  525. ret = dln2_spi_enable(dln2, false);
  526. if (ret < 0)
  527. return ret;
  528. if (dln2->speed != speed) {
  529. ret = dln2_spi_set_speed(dln2, speed);
  530. if (ret < 0)
  531. return ret;
  532. dln2->speed = speed;
  533. }
  534. if (dln2->mode != mode) {
  535. ret = dln2_spi_set_mode(dln2, mode & 0x3);
  536. if (ret < 0)
  537. return ret;
  538. dln2->mode = mode;
  539. }
  540. if (dln2->bpw != bpw) {
  541. ret = dln2_spi_set_bpw(dln2, bpw);
  542. if (ret < 0)
  543. return ret;
  544. dln2->bpw = bpw;
  545. }
  546. return dln2_spi_enable(dln2, true);
  547. }
  548. static int dln2_spi_transfer_one(struct spi_master *master,
  549. struct spi_device *spi,
  550. struct spi_transfer *xfer)
  551. {
  552. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  553. int status;
  554. u8 attr = 0;
  555. status = dln2_spi_transfer_setup(dln2, xfer->speed_hz,
  556. xfer->bits_per_word,
  557. spi->mode);
  558. if (status < 0) {
  559. dev_err(&dln2->pdev->dev, "Cannot setup transfer\n");
  560. return status;
  561. }
  562. if (!xfer->cs_change && !spi_transfer_is_last(master, xfer))
  563. attr = DLN2_SPI_ATTR_LEAVE_SS_LOW;
  564. status = dln2_spi_rdwr(dln2, xfer->tx_buf, xfer->rx_buf,
  565. xfer->len, attr);
  566. if (status < 0)
  567. dev_err(&dln2->pdev->dev, "write/read failed!\n");
  568. return status;
  569. }
  570. static int dln2_spi_probe(struct platform_device *pdev)
  571. {
  572. struct spi_master *master;
  573. struct dln2_spi *dln2;
  574. struct dln2_platform_data *pdata = dev_get_platdata(&pdev->dev);
  575. int ret;
  576. master = spi_alloc_master(&pdev->dev, sizeof(*dln2));
  577. if (!master)
  578. return -ENOMEM;
  579. platform_set_drvdata(pdev, master);
  580. dln2 = spi_master_get_devdata(master);
  581. dln2->buf = devm_kmalloc(&pdev->dev, DLN2_SPI_BUF_SIZE, GFP_KERNEL);
  582. if (!dln2->buf) {
  583. ret = -ENOMEM;
  584. goto exit_free_master;
  585. }
  586. dln2->master = master;
  587. dln2->pdev = pdev;
  588. dln2->port = pdata->port;
  589. /* cs/mode can never be 0xff, so the first transfer will set them */
  590. dln2->cs = 0xff;
  591. dln2->mode = 0xff;
  592. /* disable SPI module before continuing with the setup */
  593. ret = dln2_spi_enable(dln2, false);
  594. if (ret < 0) {
  595. dev_err(&pdev->dev, "Failed to disable SPI module\n");
  596. goto exit_free_master;
  597. }
  598. ret = dln2_spi_get_cs_num(dln2, &master->num_chipselect);
  599. if (ret < 0) {
  600. dev_err(&pdev->dev, "Failed to get number of CS pins\n");
  601. goto exit_free_master;
  602. }
  603. ret = dln2_spi_get_speed_range(dln2,
  604. &master->min_speed_hz,
  605. &master->max_speed_hz);
  606. if (ret < 0) {
  607. dev_err(&pdev->dev, "Failed to read bus min/max freqs\n");
  608. goto exit_free_master;
  609. }
  610. ret = dln2_spi_get_supported_frame_sizes(dln2,
  611. &master->bits_per_word_mask);
  612. if (ret < 0) {
  613. dev_err(&pdev->dev, "Failed to read supported frame sizes\n");
  614. goto exit_free_master;
  615. }
  616. ret = dln2_spi_cs_enable_all(dln2, true);
  617. if (ret < 0) {
  618. dev_err(&pdev->dev, "Failed to enable CS pins\n");
  619. goto exit_free_master;
  620. }
  621. master->bus_num = -1;
  622. master->mode_bits = SPI_CPOL | SPI_CPHA;
  623. master->prepare_message = dln2_spi_prepare_message;
  624. master->transfer_one = dln2_spi_transfer_one;
  625. master->auto_runtime_pm = true;
  626. /* enable SPI module, we're good to go */
  627. ret = dln2_spi_enable(dln2, true);
  628. if (ret < 0) {
  629. dev_err(&pdev->dev, "Failed to enable SPI module\n");
  630. goto exit_free_master;
  631. }
  632. pm_runtime_set_autosuspend_delay(&pdev->dev,
  633. DLN2_RPM_AUTOSUSPEND_TIMEOUT);
  634. pm_runtime_use_autosuspend(&pdev->dev);
  635. pm_runtime_set_active(&pdev->dev);
  636. pm_runtime_enable(&pdev->dev);
  637. ret = devm_spi_register_master(&pdev->dev, master);
  638. if (ret < 0) {
  639. dev_err(&pdev->dev, "Failed to register master\n");
  640. goto exit_register;
  641. }
  642. return ret;
  643. exit_register:
  644. pm_runtime_disable(&pdev->dev);
  645. pm_runtime_set_suspended(&pdev->dev);
  646. if (dln2_spi_enable(dln2, false) < 0)
  647. dev_err(&pdev->dev, "Failed to disable SPI module\n");
  648. exit_free_master:
  649. spi_master_put(master);
  650. return ret;
  651. }
  652. static int dln2_spi_remove(struct platform_device *pdev)
  653. {
  654. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  655. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  656. pm_runtime_disable(&pdev->dev);
  657. if (dln2_spi_enable(dln2, false) < 0)
  658. dev_err(&pdev->dev, "Failed to disable SPI module\n");
  659. return 0;
  660. }
  661. #ifdef CONFIG_PM_SLEEP
  662. static int dln2_spi_suspend(struct device *dev)
  663. {
  664. int ret;
  665. struct spi_master *master = dev_get_drvdata(dev);
  666. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  667. ret = spi_master_suspend(master);
  668. if (ret < 0)
  669. return ret;
  670. if (!pm_runtime_suspended(dev)) {
  671. ret = dln2_spi_enable(dln2, false);
  672. if (ret < 0)
  673. return ret;
  674. }
  675. /*
  676. * USB power may be cut off during sleep. Resetting the following
  677. * parameters will force the board to be set up before first transfer.
  678. */
  679. dln2->cs = 0xff;
  680. dln2->speed = 0;
  681. dln2->bpw = 0;
  682. dln2->mode = 0xff;
  683. return 0;
  684. }
  685. static int dln2_spi_resume(struct device *dev)
  686. {
  687. int ret;
  688. struct spi_master *master = dev_get_drvdata(dev);
  689. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  690. if (!pm_runtime_suspended(dev)) {
  691. ret = dln2_spi_cs_enable_all(dln2, true);
  692. if (ret < 0)
  693. return ret;
  694. ret = dln2_spi_enable(dln2, true);
  695. if (ret < 0)
  696. return ret;
  697. }
  698. return spi_master_resume(master);
  699. }
  700. #endif /* CONFIG_PM_SLEEP */
  701. #ifdef CONFIG_PM
  702. static int dln2_spi_runtime_suspend(struct device *dev)
  703. {
  704. struct spi_master *master = dev_get_drvdata(dev);
  705. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  706. return dln2_spi_enable(dln2, false);
  707. }
  708. static int dln2_spi_runtime_resume(struct device *dev)
  709. {
  710. struct spi_master *master = dev_get_drvdata(dev);
  711. struct dln2_spi *dln2 = spi_master_get_devdata(master);
  712. return dln2_spi_enable(dln2, true);
  713. }
  714. #endif /* CONFIG_PM */
  715. static const struct dev_pm_ops dln2_spi_pm = {
  716. SET_SYSTEM_SLEEP_PM_OPS(dln2_spi_suspend, dln2_spi_resume)
  717. SET_RUNTIME_PM_OPS(dln2_spi_runtime_suspend,
  718. dln2_spi_runtime_resume, NULL)
  719. };
  720. static struct platform_driver spi_dln2_driver = {
  721. .driver = {
  722. .name = "dln2-spi",
  723. .pm = &dln2_spi_pm,
  724. },
  725. .probe = dln2_spi_probe,
  726. .remove = dln2_spi_remove,
  727. };
  728. module_platform_driver(spi_dln2_driver);
  729. MODULE_DESCRIPTION("Driver for the Diolan DLN2 SPI master interface");
  730. MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
  731. MODULE_LICENSE("GPL v2");
  732. MODULE_ALIAS("platform:dln2-spi");