spi-pxa2xx.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/slab.h>
  31. #include <linux/clk.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/acpi.h>
  34. #include "spi-pxa2xx.h"
  35. MODULE_AUTHOR("Stephen Street");
  36. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  37. MODULE_LICENSE("GPL");
  38. MODULE_ALIAS("platform:pxa2xx-spi");
  39. #define TIMOUT_DFLT 1000
  40. /*
  41. * for testing SSCR1 changes that require SSP restart, basically
  42. * everything except the service and interrupt enables, the pxa270 developer
  43. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  44. * list, but the PXA255 dev man says all bits without really meaning the
  45. * service and interrupt enables
  46. */
  47. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  48. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  49. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  50. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  51. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  52. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  53. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  54. | QUARK_X1000_SSCR1_EFWR \
  55. | QUARK_X1000_SSCR1_RFT \
  56. | QUARK_X1000_SSCR1_TFT \
  57. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  58. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  59. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  60. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  61. #define LPSS_CAPS_CS_EN_SHIFT 9
  62. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  63. struct lpss_config {
  64. /* LPSS offset from drv_data->ioaddr */
  65. unsigned offset;
  66. /* Register offsets from drv_data->lpss_base or -1 */
  67. int reg_general;
  68. int reg_ssp;
  69. int reg_cs_ctrl;
  70. int reg_capabilities;
  71. /* FIFO thresholds */
  72. u32 rx_threshold;
  73. u32 tx_threshold_lo;
  74. u32 tx_threshold_hi;
  75. /* Chip select control */
  76. unsigned cs_sel_shift;
  77. unsigned cs_sel_mask;
  78. unsigned cs_num;
  79. };
  80. /* Keep these sorted with enum pxa_ssp_type */
  81. static const struct lpss_config lpss_platforms[] = {
  82. { /* LPSS_LPT_SSP */
  83. .offset = 0x800,
  84. .reg_general = 0x08,
  85. .reg_ssp = 0x0c,
  86. .reg_cs_ctrl = 0x18,
  87. .reg_capabilities = -1,
  88. .rx_threshold = 64,
  89. .tx_threshold_lo = 160,
  90. .tx_threshold_hi = 224,
  91. },
  92. { /* LPSS_BYT_SSP */
  93. .offset = 0x400,
  94. .reg_general = 0x08,
  95. .reg_ssp = 0x0c,
  96. .reg_cs_ctrl = 0x18,
  97. .reg_capabilities = -1,
  98. .rx_threshold = 64,
  99. .tx_threshold_lo = 160,
  100. .tx_threshold_hi = 224,
  101. },
  102. { /* LPSS_BSW_SSP */
  103. .offset = 0x400,
  104. .reg_general = 0x08,
  105. .reg_ssp = 0x0c,
  106. .reg_cs_ctrl = 0x18,
  107. .reg_capabilities = -1,
  108. .rx_threshold = 64,
  109. .tx_threshold_lo = 160,
  110. .tx_threshold_hi = 224,
  111. .cs_sel_shift = 2,
  112. .cs_sel_mask = 1 << 2,
  113. .cs_num = 2,
  114. },
  115. { /* LPSS_SPT_SSP */
  116. .offset = 0x200,
  117. .reg_general = -1,
  118. .reg_ssp = 0x20,
  119. .reg_cs_ctrl = 0x24,
  120. .reg_capabilities = -1,
  121. .rx_threshold = 1,
  122. .tx_threshold_lo = 32,
  123. .tx_threshold_hi = 56,
  124. },
  125. { /* LPSS_BXT_SSP */
  126. .offset = 0x200,
  127. .reg_general = -1,
  128. .reg_ssp = 0x20,
  129. .reg_cs_ctrl = 0x24,
  130. .reg_capabilities = 0xfc,
  131. .rx_threshold = 1,
  132. .tx_threshold_lo = 16,
  133. .tx_threshold_hi = 48,
  134. .cs_sel_shift = 8,
  135. .cs_sel_mask = 3 << 8,
  136. },
  137. };
  138. static inline const struct lpss_config
  139. *lpss_get_config(const struct driver_data *drv_data)
  140. {
  141. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  142. }
  143. static bool is_lpss_ssp(const struct driver_data *drv_data)
  144. {
  145. switch (drv_data->ssp_type) {
  146. case LPSS_LPT_SSP:
  147. case LPSS_BYT_SSP:
  148. case LPSS_BSW_SSP:
  149. case LPSS_SPT_SSP:
  150. case LPSS_BXT_SSP:
  151. return true;
  152. default:
  153. return false;
  154. }
  155. }
  156. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  157. {
  158. return drv_data->ssp_type == QUARK_X1000_SSP;
  159. }
  160. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  161. {
  162. switch (drv_data->ssp_type) {
  163. case QUARK_X1000_SSP:
  164. return QUARK_X1000_SSCR1_CHANGE_MASK;
  165. default:
  166. return SSCR1_CHANGE_MASK;
  167. }
  168. }
  169. static u32
  170. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  171. {
  172. switch (drv_data->ssp_type) {
  173. case QUARK_X1000_SSP:
  174. return RX_THRESH_QUARK_X1000_DFLT;
  175. default:
  176. return RX_THRESH_DFLT;
  177. }
  178. }
  179. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  180. {
  181. u32 mask;
  182. switch (drv_data->ssp_type) {
  183. case QUARK_X1000_SSP:
  184. mask = QUARK_X1000_SSSR_TFL_MASK;
  185. break;
  186. default:
  187. mask = SSSR_TFL_MASK;
  188. break;
  189. }
  190. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  191. }
  192. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  193. u32 *sccr1_reg)
  194. {
  195. u32 mask;
  196. switch (drv_data->ssp_type) {
  197. case QUARK_X1000_SSP:
  198. mask = QUARK_X1000_SSCR1_RFT;
  199. break;
  200. default:
  201. mask = SSCR1_RFT;
  202. break;
  203. }
  204. *sccr1_reg &= ~mask;
  205. }
  206. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  207. u32 *sccr1_reg, u32 threshold)
  208. {
  209. switch (drv_data->ssp_type) {
  210. case QUARK_X1000_SSP:
  211. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  212. break;
  213. default:
  214. *sccr1_reg |= SSCR1_RxTresh(threshold);
  215. break;
  216. }
  217. }
  218. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  219. u32 clk_div, u8 bits)
  220. {
  221. switch (drv_data->ssp_type) {
  222. case QUARK_X1000_SSP:
  223. return clk_div
  224. | QUARK_X1000_SSCR0_Motorola
  225. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  226. | SSCR0_SSE;
  227. default:
  228. return clk_div
  229. | SSCR0_Motorola
  230. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  231. | SSCR0_SSE
  232. | (bits > 16 ? SSCR0_EDSS : 0);
  233. }
  234. }
  235. /*
  236. * Read and write LPSS SSP private registers. Caller must first check that
  237. * is_lpss_ssp() returns true before these can be called.
  238. */
  239. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  240. {
  241. WARN_ON(!drv_data->lpss_base);
  242. return readl(drv_data->lpss_base + offset);
  243. }
  244. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  245. unsigned offset, u32 value)
  246. {
  247. WARN_ON(!drv_data->lpss_base);
  248. writel(value, drv_data->lpss_base + offset);
  249. }
  250. /*
  251. * lpss_ssp_setup - perform LPSS SSP specific setup
  252. * @drv_data: pointer to the driver private data
  253. *
  254. * Perform LPSS SSP specific setup. This function must be called first if
  255. * one is going to use LPSS SSP private registers.
  256. */
  257. static void lpss_ssp_setup(struct driver_data *drv_data)
  258. {
  259. const struct lpss_config *config;
  260. u32 value;
  261. config = lpss_get_config(drv_data);
  262. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  263. /* Enable software chip select control */
  264. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  265. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  266. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  267. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  268. /* Enable multiblock DMA transfers */
  269. if (drv_data->master_info->enable_dma) {
  270. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  271. if (config->reg_general >= 0) {
  272. value = __lpss_ssp_read_priv(drv_data,
  273. config->reg_general);
  274. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  275. __lpss_ssp_write_priv(drv_data,
  276. config->reg_general, value);
  277. }
  278. }
  279. }
  280. static void lpss_ssp_select_cs(struct driver_data *drv_data,
  281. const struct lpss_config *config)
  282. {
  283. u32 value, cs;
  284. if (!config->cs_sel_mask)
  285. return;
  286. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  287. cs = drv_data->cur_msg->spi->chip_select;
  288. cs <<= config->cs_sel_shift;
  289. if (cs != (value & config->cs_sel_mask)) {
  290. /*
  291. * When switching another chip select output active the
  292. * output must be selected first and wait 2 ssp_clk cycles
  293. * before changing state to active. Otherwise a short
  294. * glitch will occur on the previous chip select since
  295. * output select is latched but state control is not.
  296. */
  297. value &= ~config->cs_sel_mask;
  298. value |= cs;
  299. __lpss_ssp_write_priv(drv_data,
  300. config->reg_cs_ctrl, value);
  301. ndelay(1000000000 /
  302. (drv_data->master->max_speed_hz / 2));
  303. }
  304. }
  305. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  306. {
  307. const struct lpss_config *config;
  308. u32 value;
  309. config = lpss_get_config(drv_data);
  310. if (enable)
  311. lpss_ssp_select_cs(drv_data, config);
  312. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  313. if (enable)
  314. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  315. else
  316. value |= LPSS_CS_CONTROL_CS_HIGH;
  317. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  318. }
  319. static void cs_assert(struct driver_data *drv_data)
  320. {
  321. struct chip_data *chip = drv_data->cur_chip;
  322. if (drv_data->ssp_type == CE4100_SSP) {
  323. pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
  324. return;
  325. }
  326. if (chip->cs_control) {
  327. chip->cs_control(PXA2XX_CS_ASSERT);
  328. return;
  329. }
  330. if (gpio_is_valid(chip->gpio_cs)) {
  331. gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
  332. return;
  333. }
  334. if (is_lpss_ssp(drv_data))
  335. lpss_ssp_cs_control(drv_data, true);
  336. }
  337. static void cs_deassert(struct driver_data *drv_data)
  338. {
  339. struct chip_data *chip = drv_data->cur_chip;
  340. if (drv_data->ssp_type == CE4100_SSP)
  341. return;
  342. if (chip->cs_control) {
  343. chip->cs_control(PXA2XX_CS_DEASSERT);
  344. return;
  345. }
  346. if (gpio_is_valid(chip->gpio_cs)) {
  347. gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
  348. return;
  349. }
  350. if (is_lpss_ssp(drv_data))
  351. lpss_ssp_cs_control(drv_data, false);
  352. }
  353. int pxa2xx_spi_flush(struct driver_data *drv_data)
  354. {
  355. unsigned long limit = loops_per_jiffy << 1;
  356. do {
  357. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  358. pxa2xx_spi_read(drv_data, SSDR);
  359. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  360. write_SSSR_CS(drv_data, SSSR_ROR);
  361. return limit;
  362. }
  363. static int null_writer(struct driver_data *drv_data)
  364. {
  365. u8 n_bytes = drv_data->n_bytes;
  366. if (pxa2xx_spi_txfifo_full(drv_data)
  367. || (drv_data->tx == drv_data->tx_end))
  368. return 0;
  369. pxa2xx_spi_write(drv_data, SSDR, 0);
  370. drv_data->tx += n_bytes;
  371. return 1;
  372. }
  373. static int null_reader(struct driver_data *drv_data)
  374. {
  375. u8 n_bytes = drv_data->n_bytes;
  376. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  377. && (drv_data->rx < drv_data->rx_end)) {
  378. pxa2xx_spi_read(drv_data, SSDR);
  379. drv_data->rx += n_bytes;
  380. }
  381. return drv_data->rx == drv_data->rx_end;
  382. }
  383. static int u8_writer(struct driver_data *drv_data)
  384. {
  385. if (pxa2xx_spi_txfifo_full(drv_data)
  386. || (drv_data->tx == drv_data->tx_end))
  387. return 0;
  388. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  389. ++drv_data->tx;
  390. return 1;
  391. }
  392. static int u8_reader(struct driver_data *drv_data)
  393. {
  394. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  395. && (drv_data->rx < drv_data->rx_end)) {
  396. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  397. ++drv_data->rx;
  398. }
  399. return drv_data->rx == drv_data->rx_end;
  400. }
  401. static int u16_writer(struct driver_data *drv_data)
  402. {
  403. if (pxa2xx_spi_txfifo_full(drv_data)
  404. || (drv_data->tx == drv_data->tx_end))
  405. return 0;
  406. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  407. drv_data->tx += 2;
  408. return 1;
  409. }
  410. static int u16_reader(struct driver_data *drv_data)
  411. {
  412. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  413. && (drv_data->rx < drv_data->rx_end)) {
  414. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  415. drv_data->rx += 2;
  416. }
  417. return drv_data->rx == drv_data->rx_end;
  418. }
  419. static int u32_writer(struct driver_data *drv_data)
  420. {
  421. if (pxa2xx_spi_txfifo_full(drv_data)
  422. || (drv_data->tx == drv_data->tx_end))
  423. return 0;
  424. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  425. drv_data->tx += 4;
  426. return 1;
  427. }
  428. static int u32_reader(struct driver_data *drv_data)
  429. {
  430. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  431. && (drv_data->rx < drv_data->rx_end)) {
  432. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  433. drv_data->rx += 4;
  434. }
  435. return drv_data->rx == drv_data->rx_end;
  436. }
  437. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  438. {
  439. struct spi_message *msg = drv_data->cur_msg;
  440. struct spi_transfer *trans = drv_data->cur_transfer;
  441. /* Move to next transfer */
  442. if (trans->transfer_list.next != &msg->transfers) {
  443. drv_data->cur_transfer =
  444. list_entry(trans->transfer_list.next,
  445. struct spi_transfer,
  446. transfer_list);
  447. return RUNNING_STATE;
  448. } else
  449. return DONE_STATE;
  450. }
  451. /* caller already set message->status; dma and pio irqs are blocked */
  452. static void giveback(struct driver_data *drv_data)
  453. {
  454. struct spi_transfer* last_transfer;
  455. struct spi_message *msg;
  456. unsigned long timeout;
  457. msg = drv_data->cur_msg;
  458. drv_data->cur_msg = NULL;
  459. drv_data->cur_transfer = NULL;
  460. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  461. transfer_list);
  462. /* Delay if requested before any change in chip select */
  463. if (last_transfer->delay_usecs)
  464. udelay(last_transfer->delay_usecs);
  465. /* Wait until SSP becomes idle before deasserting the CS */
  466. timeout = jiffies + msecs_to_jiffies(10);
  467. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  468. !time_after(jiffies, timeout))
  469. cpu_relax();
  470. /* Drop chip select UNLESS cs_change is true or we are returning
  471. * a message with an error, or next message is for another chip
  472. */
  473. if (!last_transfer->cs_change)
  474. cs_deassert(drv_data);
  475. else {
  476. struct spi_message *next_msg;
  477. /* Holding of cs was hinted, but we need to make sure
  478. * the next message is for the same chip. Don't waste
  479. * time with the following tests unless this was hinted.
  480. *
  481. * We cannot postpone this until pump_messages, because
  482. * after calling msg->complete (below) the driver that
  483. * sent the current message could be unloaded, which
  484. * could invalidate the cs_control() callback...
  485. */
  486. /* get a pointer to the next message, if any */
  487. next_msg = spi_get_next_queued_message(drv_data->master);
  488. /* see if the next and current messages point
  489. * to the same chip
  490. */
  491. if ((next_msg && next_msg->spi != msg->spi) ||
  492. msg->state == ERROR_STATE)
  493. cs_deassert(drv_data);
  494. }
  495. drv_data->cur_chip = NULL;
  496. spi_finalize_current_message(drv_data->master);
  497. }
  498. static void reset_sccr1(struct driver_data *drv_data)
  499. {
  500. struct chip_data *chip = drv_data->cur_chip;
  501. u32 sccr1_reg;
  502. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  503. switch (drv_data->ssp_type) {
  504. case QUARK_X1000_SSP:
  505. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  506. break;
  507. default:
  508. sccr1_reg &= ~SSCR1_RFT;
  509. break;
  510. }
  511. sccr1_reg |= chip->threshold;
  512. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  513. }
  514. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  515. {
  516. /* Stop and reset SSP */
  517. write_SSSR_CS(drv_data, drv_data->clear_sr);
  518. reset_sccr1(drv_data);
  519. if (!pxa25x_ssp_comp(drv_data))
  520. pxa2xx_spi_write(drv_data, SSTO, 0);
  521. pxa2xx_spi_flush(drv_data);
  522. pxa2xx_spi_write(drv_data, SSCR0,
  523. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  524. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  525. drv_data->cur_msg->state = ERROR_STATE;
  526. tasklet_schedule(&drv_data->pump_transfers);
  527. }
  528. static void int_transfer_complete(struct driver_data *drv_data)
  529. {
  530. /* Clear and disable interrupts */
  531. write_SSSR_CS(drv_data, drv_data->clear_sr);
  532. reset_sccr1(drv_data);
  533. if (!pxa25x_ssp_comp(drv_data))
  534. pxa2xx_spi_write(drv_data, SSTO, 0);
  535. /* Update total byte transferred return count actual bytes read */
  536. drv_data->cur_msg->actual_length += drv_data->len -
  537. (drv_data->rx_end - drv_data->rx);
  538. /* Transfer delays and chip select release are
  539. * handled in pump_transfers or giveback
  540. */
  541. /* Move to next transfer */
  542. drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  543. /* Schedule transfer tasklet */
  544. tasklet_schedule(&drv_data->pump_transfers);
  545. }
  546. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  547. {
  548. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  549. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  550. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  551. if (irq_status & SSSR_ROR) {
  552. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  553. return IRQ_HANDLED;
  554. }
  555. if (irq_status & SSSR_TINT) {
  556. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  557. if (drv_data->read(drv_data)) {
  558. int_transfer_complete(drv_data);
  559. return IRQ_HANDLED;
  560. }
  561. }
  562. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  563. do {
  564. if (drv_data->read(drv_data)) {
  565. int_transfer_complete(drv_data);
  566. return IRQ_HANDLED;
  567. }
  568. } while (drv_data->write(drv_data));
  569. if (drv_data->read(drv_data)) {
  570. int_transfer_complete(drv_data);
  571. return IRQ_HANDLED;
  572. }
  573. if (drv_data->tx == drv_data->tx_end) {
  574. u32 bytes_left;
  575. u32 sccr1_reg;
  576. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  577. sccr1_reg &= ~SSCR1_TIE;
  578. /*
  579. * PXA25x_SSP has no timeout, set up rx threshould for the
  580. * remaining RX bytes.
  581. */
  582. if (pxa25x_ssp_comp(drv_data)) {
  583. u32 rx_thre;
  584. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  585. bytes_left = drv_data->rx_end - drv_data->rx;
  586. switch (drv_data->n_bytes) {
  587. case 4:
  588. bytes_left >>= 1;
  589. case 2:
  590. bytes_left >>= 1;
  591. }
  592. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  593. if (rx_thre > bytes_left)
  594. rx_thre = bytes_left;
  595. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  596. }
  597. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  598. }
  599. /* We did something */
  600. return IRQ_HANDLED;
  601. }
  602. static irqreturn_t ssp_int(int irq, void *dev_id)
  603. {
  604. struct driver_data *drv_data = dev_id;
  605. u32 sccr1_reg;
  606. u32 mask = drv_data->mask_sr;
  607. u32 status;
  608. /*
  609. * The IRQ might be shared with other peripherals so we must first
  610. * check that are we RPM suspended or not. If we are we assume that
  611. * the IRQ was not for us (we shouldn't be RPM suspended when the
  612. * interrupt is enabled).
  613. */
  614. if (pm_runtime_suspended(&drv_data->pdev->dev))
  615. return IRQ_NONE;
  616. /*
  617. * If the device is not yet in RPM suspended state and we get an
  618. * interrupt that is meant for another device, check if status bits
  619. * are all set to one. That means that the device is already
  620. * powered off.
  621. */
  622. status = pxa2xx_spi_read(drv_data, SSSR);
  623. if (status == ~0)
  624. return IRQ_NONE;
  625. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  626. /* Ignore possible writes if we don't need to write */
  627. if (!(sccr1_reg & SSCR1_TIE))
  628. mask &= ~SSSR_TFS;
  629. /* Ignore RX timeout interrupt if it is disabled */
  630. if (!(sccr1_reg & SSCR1_TINTE))
  631. mask &= ~SSSR_TINT;
  632. if (!(status & mask))
  633. return IRQ_NONE;
  634. if (!drv_data->cur_msg) {
  635. pxa2xx_spi_write(drv_data, SSCR0,
  636. pxa2xx_spi_read(drv_data, SSCR0)
  637. & ~SSCR0_SSE);
  638. pxa2xx_spi_write(drv_data, SSCR1,
  639. pxa2xx_spi_read(drv_data, SSCR1)
  640. & ~drv_data->int_cr1);
  641. if (!pxa25x_ssp_comp(drv_data))
  642. pxa2xx_spi_write(drv_data, SSTO, 0);
  643. write_SSSR_CS(drv_data, drv_data->clear_sr);
  644. dev_err(&drv_data->pdev->dev,
  645. "bad message state in interrupt handler\n");
  646. /* Never fail */
  647. return IRQ_HANDLED;
  648. }
  649. return drv_data->transfer_handler(drv_data);
  650. }
  651. /*
  652. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  653. * input frequency by fractions of 2^24. It also has a divider by 5.
  654. *
  655. * There are formulas to get baud rate value for given input frequency and
  656. * divider parameters, such as DDS_CLK_RATE and SCR:
  657. *
  658. * Fsys = 200MHz
  659. *
  660. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  661. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  662. *
  663. * DDS_CLK_RATE either 2^n or 2^n / 5.
  664. * SCR is in range 0 .. 255
  665. *
  666. * Divisor = 5^i * 2^j * 2 * k
  667. * i = [0, 1] i = 1 iff j = 0 or j > 3
  668. * j = [0, 23] j = 0 iff i = 1
  669. * k = [1, 256]
  670. * Special case: j = 0, i = 1: Divisor = 2 / 5
  671. *
  672. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  673. * are:
  674. * Case 1: 2^n, n = [0, 23]
  675. * Case 2: 2^24 * 2 / 5 (0x666666)
  676. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  677. *
  678. * In all cases the lowest possible value is better.
  679. *
  680. * The function calculates parameters for all cases and chooses the one closest
  681. * to the asked baud rate.
  682. */
  683. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  684. {
  685. unsigned long xtal = 200000000;
  686. unsigned long fref = xtal / 2; /* mandatory division by 2,
  687. see (2) */
  688. /* case 3 */
  689. unsigned long fref1 = fref / 2; /* case 1 */
  690. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  691. unsigned long scale;
  692. unsigned long q, q1, q2;
  693. long r, r1, r2;
  694. u32 mul;
  695. /* Case 1 */
  696. /* Set initial value for DDS_CLK_RATE */
  697. mul = (1 << 24) >> 1;
  698. /* Calculate initial quot */
  699. q1 = DIV_ROUND_UP(fref1, rate);
  700. /* Scale q1 if it's too big */
  701. if (q1 > 256) {
  702. /* Scale q1 to range [1, 512] */
  703. scale = fls_long(q1 - 1);
  704. if (scale > 9) {
  705. q1 >>= scale - 9;
  706. mul >>= scale - 9;
  707. }
  708. /* Round the result if we have a remainder */
  709. q1 += q1 & 1;
  710. }
  711. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  712. scale = __ffs(q1);
  713. q1 >>= scale;
  714. mul >>= scale;
  715. /* Get the remainder */
  716. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  717. /* Case 2 */
  718. q2 = DIV_ROUND_UP(fref2, rate);
  719. r2 = abs(fref2 / q2 - rate);
  720. /*
  721. * Choose the best between two: less remainder we have the better. We
  722. * can't go case 2 if q2 is greater than 256 since SCR register can
  723. * hold only values 0 .. 255.
  724. */
  725. if (r2 >= r1 || q2 > 256) {
  726. /* case 1 is better */
  727. r = r1;
  728. q = q1;
  729. } else {
  730. /* case 2 is better */
  731. r = r2;
  732. q = q2;
  733. mul = (1 << 24) * 2 / 5;
  734. }
  735. /* Check case 3 only if the divisor is big enough */
  736. if (fref / rate >= 80) {
  737. u64 fssp;
  738. u32 m;
  739. /* Calculate initial quot */
  740. q1 = DIV_ROUND_UP(fref, rate);
  741. m = (1 << 24) / q1;
  742. /* Get the remainder */
  743. fssp = (u64)fref * m;
  744. do_div(fssp, 1 << 24);
  745. r1 = abs(fssp - rate);
  746. /* Choose this one if it suits better */
  747. if (r1 < r) {
  748. /* case 3 is better */
  749. q = 1;
  750. mul = m;
  751. }
  752. }
  753. *dds = mul;
  754. return q - 1;
  755. }
  756. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  757. {
  758. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  759. const struct ssp_device *ssp = drv_data->ssp;
  760. rate = min_t(int, ssp_clk, rate);
  761. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  762. return (ssp_clk / (2 * rate) - 1) & 0xff;
  763. else
  764. return (ssp_clk / rate - 1) & 0xfff;
  765. }
  766. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  767. int rate)
  768. {
  769. struct chip_data *chip = drv_data->cur_chip;
  770. unsigned int clk_div;
  771. switch (drv_data->ssp_type) {
  772. case QUARK_X1000_SSP:
  773. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  774. break;
  775. default:
  776. clk_div = ssp_get_clk_div(drv_data, rate);
  777. break;
  778. }
  779. return clk_div << 8;
  780. }
  781. static bool pxa2xx_spi_can_dma(struct spi_master *master,
  782. struct spi_device *spi,
  783. struct spi_transfer *xfer)
  784. {
  785. struct chip_data *chip = spi_get_ctldata(spi);
  786. return chip->enable_dma &&
  787. xfer->len <= MAX_DMA_LEN &&
  788. xfer->len >= chip->dma_burst_size;
  789. }
  790. static void pump_transfers(unsigned long data)
  791. {
  792. struct driver_data *drv_data = (struct driver_data *)data;
  793. struct spi_master *master = drv_data->master;
  794. struct spi_message *message = NULL;
  795. struct spi_transfer *transfer = NULL;
  796. struct spi_transfer *previous = NULL;
  797. struct chip_data *chip = NULL;
  798. u32 clk_div = 0;
  799. u8 bits = 0;
  800. u32 speed = 0;
  801. u32 cr0;
  802. u32 cr1;
  803. u32 dma_thresh = drv_data->cur_chip->dma_threshold;
  804. u32 dma_burst = drv_data->cur_chip->dma_burst_size;
  805. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  806. int err;
  807. int dma_mapped;
  808. /* Get current state information */
  809. message = drv_data->cur_msg;
  810. transfer = drv_data->cur_transfer;
  811. chip = drv_data->cur_chip;
  812. /* Handle for abort */
  813. if (message->state == ERROR_STATE) {
  814. message->status = -EIO;
  815. giveback(drv_data);
  816. return;
  817. }
  818. /* Handle end of message */
  819. if (message->state == DONE_STATE) {
  820. message->status = 0;
  821. giveback(drv_data);
  822. return;
  823. }
  824. /* Delay if requested at end of transfer before CS change */
  825. if (message->state == RUNNING_STATE) {
  826. previous = list_entry(transfer->transfer_list.prev,
  827. struct spi_transfer,
  828. transfer_list);
  829. if (previous->delay_usecs)
  830. udelay(previous->delay_usecs);
  831. /* Drop chip select only if cs_change is requested */
  832. if (previous->cs_change)
  833. cs_deassert(drv_data);
  834. }
  835. /* Check if we can DMA this transfer */
  836. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  837. /* reject already-mapped transfers; PIO won't always work */
  838. if (message->is_dma_mapped
  839. || transfer->rx_dma || transfer->tx_dma) {
  840. dev_err(&drv_data->pdev->dev,
  841. "pump_transfers: mapped transfer length of "
  842. "%u is greater than %d\n",
  843. transfer->len, MAX_DMA_LEN);
  844. message->status = -EINVAL;
  845. giveback(drv_data);
  846. return;
  847. }
  848. /* warn ... we force this to PIO mode */
  849. dev_warn_ratelimited(&message->spi->dev,
  850. "pump_transfers: DMA disabled for transfer length %ld "
  851. "greater than %d\n",
  852. (long)drv_data->len, MAX_DMA_LEN);
  853. }
  854. /* Setup the transfer state based on the type of transfer */
  855. if (pxa2xx_spi_flush(drv_data) == 0) {
  856. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  857. message->status = -EIO;
  858. giveback(drv_data);
  859. return;
  860. }
  861. drv_data->n_bytes = chip->n_bytes;
  862. drv_data->tx = (void *)transfer->tx_buf;
  863. drv_data->tx_end = drv_data->tx + transfer->len;
  864. drv_data->rx = transfer->rx_buf;
  865. drv_data->rx_end = drv_data->rx + transfer->len;
  866. drv_data->len = transfer->len;
  867. drv_data->write = drv_data->tx ? chip->write : null_writer;
  868. drv_data->read = drv_data->rx ? chip->read : null_reader;
  869. /* Change speed and bit per word on a per transfer */
  870. bits = transfer->bits_per_word;
  871. speed = transfer->speed_hz;
  872. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  873. if (bits <= 8) {
  874. drv_data->n_bytes = 1;
  875. drv_data->read = drv_data->read != null_reader ?
  876. u8_reader : null_reader;
  877. drv_data->write = drv_data->write != null_writer ?
  878. u8_writer : null_writer;
  879. } else if (bits <= 16) {
  880. drv_data->n_bytes = 2;
  881. drv_data->read = drv_data->read != null_reader ?
  882. u16_reader : null_reader;
  883. drv_data->write = drv_data->write != null_writer ?
  884. u16_writer : null_writer;
  885. } else if (bits <= 32) {
  886. drv_data->n_bytes = 4;
  887. drv_data->read = drv_data->read != null_reader ?
  888. u32_reader : null_reader;
  889. drv_data->write = drv_data->write != null_writer ?
  890. u32_writer : null_writer;
  891. }
  892. /*
  893. * if bits/word is changed in dma mode, then must check the
  894. * thresholds and burst also
  895. */
  896. if (chip->enable_dma) {
  897. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  898. message->spi,
  899. bits, &dma_burst,
  900. &dma_thresh))
  901. dev_warn_ratelimited(&message->spi->dev,
  902. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  903. }
  904. message->state = RUNNING_STATE;
  905. dma_mapped = master->can_dma &&
  906. master->can_dma(master, message->spi, transfer) &&
  907. master->cur_msg_mapped;
  908. if (dma_mapped) {
  909. /* Ensure we have the correct interrupt handler */
  910. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  911. err = pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  912. if (err) {
  913. message->status = err;
  914. giveback(drv_data);
  915. return;
  916. }
  917. /* Clear status and start DMA engine */
  918. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  919. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  920. pxa2xx_spi_dma_start(drv_data);
  921. } else {
  922. /* Ensure we have the correct interrupt handler */
  923. drv_data->transfer_handler = interrupt_transfer;
  924. /* Clear status */
  925. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  926. write_SSSR_CS(drv_data, drv_data->clear_sr);
  927. }
  928. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  929. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  930. if (!pxa25x_ssp_comp(drv_data))
  931. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  932. master->max_speed_hz
  933. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  934. dma_mapped ? "DMA" : "PIO");
  935. else
  936. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  937. master->max_speed_hz / 2
  938. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  939. dma_mapped ? "DMA" : "PIO");
  940. if (is_lpss_ssp(drv_data)) {
  941. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  942. != chip->lpss_rx_threshold)
  943. pxa2xx_spi_write(drv_data, SSIRF,
  944. chip->lpss_rx_threshold);
  945. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  946. != chip->lpss_tx_threshold)
  947. pxa2xx_spi_write(drv_data, SSITF,
  948. chip->lpss_tx_threshold);
  949. }
  950. if (is_quark_x1000_ssp(drv_data) &&
  951. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  952. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  953. /* see if we need to reload the config registers */
  954. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  955. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  956. != (cr1 & change_mask)) {
  957. /* stop the SSP, and update the other bits */
  958. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  959. if (!pxa25x_ssp_comp(drv_data))
  960. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  961. /* first set CR1 without interrupt and service enables */
  962. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  963. /* restart the SSP */
  964. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  965. } else {
  966. if (!pxa25x_ssp_comp(drv_data))
  967. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  968. }
  969. cs_assert(drv_data);
  970. /* after chip select, release the data by enabling service
  971. * requests and interrupts, without changing any mode bits */
  972. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  973. }
  974. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  975. struct spi_message *msg)
  976. {
  977. struct driver_data *drv_data = spi_master_get_devdata(master);
  978. drv_data->cur_msg = msg;
  979. /* Initial message state*/
  980. drv_data->cur_msg->state = START_STATE;
  981. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  982. struct spi_transfer,
  983. transfer_list);
  984. /* prepare to setup the SSP, in pump_transfers, using the per
  985. * chip configuration */
  986. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  987. /* Mark as busy and launch transfers */
  988. tasklet_schedule(&drv_data->pump_transfers);
  989. return 0;
  990. }
  991. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  992. {
  993. struct driver_data *drv_data = spi_master_get_devdata(master);
  994. /* Disable the SSP now */
  995. pxa2xx_spi_write(drv_data, SSCR0,
  996. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  997. return 0;
  998. }
  999. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  1000. struct pxa2xx_spi_chip *chip_info)
  1001. {
  1002. int err = 0;
  1003. if (chip == NULL || chip_info == NULL)
  1004. return 0;
  1005. /* NOTE: setup() can be called multiple times, possibly with
  1006. * different chip_info, release previously requested GPIO
  1007. */
  1008. if (gpio_is_valid(chip->gpio_cs))
  1009. gpio_free(chip->gpio_cs);
  1010. /* If (*cs_control) is provided, ignore GPIO chip select */
  1011. if (chip_info->cs_control) {
  1012. chip->cs_control = chip_info->cs_control;
  1013. return 0;
  1014. }
  1015. if (gpio_is_valid(chip_info->gpio_cs)) {
  1016. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1017. if (err) {
  1018. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1019. chip_info->gpio_cs);
  1020. return err;
  1021. }
  1022. chip->gpio_cs = chip_info->gpio_cs;
  1023. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1024. err = gpio_direction_output(chip->gpio_cs,
  1025. !chip->gpio_cs_inverted);
  1026. }
  1027. return err;
  1028. }
  1029. static int setup(struct spi_device *spi)
  1030. {
  1031. struct pxa2xx_spi_chip *chip_info = NULL;
  1032. struct chip_data *chip;
  1033. const struct lpss_config *config;
  1034. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1035. uint tx_thres, tx_hi_thres, rx_thres;
  1036. switch (drv_data->ssp_type) {
  1037. case QUARK_X1000_SSP:
  1038. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1039. tx_hi_thres = 0;
  1040. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1041. break;
  1042. case LPSS_LPT_SSP:
  1043. case LPSS_BYT_SSP:
  1044. case LPSS_BSW_SSP:
  1045. case LPSS_SPT_SSP:
  1046. case LPSS_BXT_SSP:
  1047. config = lpss_get_config(drv_data);
  1048. tx_thres = config->tx_threshold_lo;
  1049. tx_hi_thres = config->tx_threshold_hi;
  1050. rx_thres = config->rx_threshold;
  1051. break;
  1052. default:
  1053. tx_thres = TX_THRESH_DFLT;
  1054. tx_hi_thres = 0;
  1055. rx_thres = RX_THRESH_DFLT;
  1056. break;
  1057. }
  1058. /* Only alloc on first setup */
  1059. chip = spi_get_ctldata(spi);
  1060. if (!chip) {
  1061. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1062. if (!chip)
  1063. return -ENOMEM;
  1064. if (drv_data->ssp_type == CE4100_SSP) {
  1065. if (spi->chip_select > 4) {
  1066. dev_err(&spi->dev,
  1067. "failed setup: cs number must not be > 4.\n");
  1068. kfree(chip);
  1069. return -EINVAL;
  1070. }
  1071. chip->frm = spi->chip_select;
  1072. } else
  1073. chip->gpio_cs = -1;
  1074. chip->enable_dma = drv_data->master_info->enable_dma;
  1075. chip->timeout = TIMOUT_DFLT;
  1076. }
  1077. /* protocol drivers may change the chip settings, so...
  1078. * if chip_info exists, use it */
  1079. chip_info = spi->controller_data;
  1080. /* chip_info isn't always needed */
  1081. chip->cr1 = 0;
  1082. if (chip_info) {
  1083. if (chip_info->timeout)
  1084. chip->timeout = chip_info->timeout;
  1085. if (chip_info->tx_threshold)
  1086. tx_thres = chip_info->tx_threshold;
  1087. if (chip_info->tx_hi_threshold)
  1088. tx_hi_thres = chip_info->tx_hi_threshold;
  1089. if (chip_info->rx_threshold)
  1090. rx_thres = chip_info->rx_threshold;
  1091. chip->dma_threshold = 0;
  1092. if (chip_info->enable_loopback)
  1093. chip->cr1 = SSCR1_LBM;
  1094. }
  1095. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1096. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1097. | SSITF_TxHiThresh(tx_hi_thres);
  1098. /* set dma burst and threshold outside of chip_info path so that if
  1099. * chip_info goes away after setting chip->enable_dma, the
  1100. * burst and threshold can still respond to changes in bits_per_word */
  1101. if (chip->enable_dma) {
  1102. /* set up legal burst and threshold for dma */
  1103. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1104. spi->bits_per_word,
  1105. &chip->dma_burst_size,
  1106. &chip->dma_threshold)) {
  1107. dev_warn(&spi->dev,
  1108. "in setup: DMA burst size reduced to match bits_per_word\n");
  1109. }
  1110. }
  1111. switch (drv_data->ssp_type) {
  1112. case QUARK_X1000_SSP:
  1113. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1114. & QUARK_X1000_SSCR1_RFT)
  1115. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1116. & QUARK_X1000_SSCR1_TFT);
  1117. break;
  1118. default:
  1119. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1120. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1121. break;
  1122. }
  1123. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1124. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1125. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1126. if (spi->mode & SPI_LOOP)
  1127. chip->cr1 |= SSCR1_LBM;
  1128. if (spi->bits_per_word <= 8) {
  1129. chip->n_bytes = 1;
  1130. chip->read = u8_reader;
  1131. chip->write = u8_writer;
  1132. } else if (spi->bits_per_word <= 16) {
  1133. chip->n_bytes = 2;
  1134. chip->read = u16_reader;
  1135. chip->write = u16_writer;
  1136. } else if (spi->bits_per_word <= 32) {
  1137. chip->n_bytes = 4;
  1138. chip->read = u32_reader;
  1139. chip->write = u32_writer;
  1140. }
  1141. spi_set_ctldata(spi, chip);
  1142. if (drv_data->ssp_type == CE4100_SSP)
  1143. return 0;
  1144. return setup_cs(spi, chip, chip_info);
  1145. }
  1146. static void cleanup(struct spi_device *spi)
  1147. {
  1148. struct chip_data *chip = spi_get_ctldata(spi);
  1149. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1150. if (!chip)
  1151. return;
  1152. if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
  1153. gpio_free(chip->gpio_cs);
  1154. kfree(chip);
  1155. }
  1156. #ifdef CONFIG_PCI
  1157. #ifdef CONFIG_ACPI
  1158. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1159. { "INT33C0", LPSS_LPT_SSP },
  1160. { "INT33C1", LPSS_LPT_SSP },
  1161. { "INT3430", LPSS_LPT_SSP },
  1162. { "INT3431", LPSS_LPT_SSP },
  1163. { "80860F0E", LPSS_BYT_SSP },
  1164. { "8086228E", LPSS_BSW_SSP },
  1165. { },
  1166. };
  1167. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1168. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1169. {
  1170. unsigned int devid;
  1171. int port_id = -1;
  1172. if (adev && adev->pnp.unique_id &&
  1173. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1174. port_id = devid;
  1175. return port_id;
  1176. }
  1177. #else /* !CONFIG_ACPI */
  1178. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1179. {
  1180. return -1;
  1181. }
  1182. #endif
  1183. /*
  1184. * PCI IDs of compound devices that integrate both host controller and private
  1185. * integrated DMA engine. Please note these are not used in module
  1186. * autoloading and probing in this module but matching the LPSS SSP type.
  1187. */
  1188. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1189. /* SPT-LP */
  1190. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1191. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1192. /* SPT-H */
  1193. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1194. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1195. /* KBL-H */
  1196. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1197. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1198. /* BXT A-Step */
  1199. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1200. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1201. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1202. /* BXT B-Step */
  1203. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1204. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1205. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1206. /* APL */
  1207. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1208. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1209. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1210. { },
  1211. };
  1212. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1213. {
  1214. struct device *dev = param;
  1215. if (dev != chan->device->dev->parent)
  1216. return false;
  1217. return true;
  1218. }
  1219. static struct pxa2xx_spi_master *
  1220. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1221. {
  1222. struct pxa2xx_spi_master *pdata;
  1223. struct acpi_device *adev;
  1224. struct ssp_device *ssp;
  1225. struct resource *res;
  1226. const struct acpi_device_id *adev_id = NULL;
  1227. const struct pci_device_id *pcidev_id = NULL;
  1228. int type;
  1229. adev = ACPI_COMPANION(&pdev->dev);
  1230. if (dev_is_pci(pdev->dev.parent))
  1231. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1232. to_pci_dev(pdev->dev.parent));
  1233. else if (adev)
  1234. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1235. &pdev->dev);
  1236. else
  1237. return NULL;
  1238. if (adev_id)
  1239. type = (int)adev_id->driver_data;
  1240. else if (pcidev_id)
  1241. type = (int)pcidev_id->driver_data;
  1242. else
  1243. return NULL;
  1244. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1245. if (!pdata)
  1246. return NULL;
  1247. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1248. if (!res)
  1249. return NULL;
  1250. ssp = &pdata->ssp;
  1251. ssp->phys_base = res->start;
  1252. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1253. if (IS_ERR(ssp->mmio_base))
  1254. return NULL;
  1255. if (pcidev_id) {
  1256. pdata->tx_param = pdev->dev.parent;
  1257. pdata->rx_param = pdev->dev.parent;
  1258. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1259. }
  1260. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1261. ssp->irq = platform_get_irq(pdev, 0);
  1262. ssp->type = type;
  1263. ssp->pdev = pdev;
  1264. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1265. pdata->num_chipselect = 1;
  1266. pdata->enable_dma = true;
  1267. return pdata;
  1268. }
  1269. #else /* !CONFIG_PCI */
  1270. static inline struct pxa2xx_spi_master *
  1271. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1272. {
  1273. return NULL;
  1274. }
  1275. #endif
  1276. static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
  1277. {
  1278. struct driver_data *drv_data = spi_master_get_devdata(master);
  1279. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1280. switch (drv_data->ssp_type) {
  1281. /*
  1282. * For Atoms the ACPI DeviceSelection used by the Windows
  1283. * driver starts from 1 instead of 0 so translate it here
  1284. * to match what Linux expects.
  1285. */
  1286. case LPSS_BYT_SSP:
  1287. case LPSS_BSW_SSP:
  1288. return cs - 1;
  1289. default:
  1290. break;
  1291. }
  1292. }
  1293. return cs;
  1294. }
  1295. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1296. {
  1297. struct device *dev = &pdev->dev;
  1298. struct pxa2xx_spi_master *platform_info;
  1299. struct spi_master *master;
  1300. struct driver_data *drv_data;
  1301. struct ssp_device *ssp;
  1302. const struct lpss_config *config;
  1303. int status;
  1304. u32 tmp;
  1305. platform_info = dev_get_platdata(dev);
  1306. if (!platform_info) {
  1307. platform_info = pxa2xx_spi_init_pdata(pdev);
  1308. if (!platform_info) {
  1309. dev_err(&pdev->dev, "missing platform data\n");
  1310. return -ENODEV;
  1311. }
  1312. }
  1313. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1314. if (!ssp)
  1315. ssp = &platform_info->ssp;
  1316. if (!ssp->mmio_base) {
  1317. dev_err(&pdev->dev, "failed to get ssp\n");
  1318. return -ENODEV;
  1319. }
  1320. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1321. if (!master) {
  1322. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1323. pxa_ssp_free(ssp);
  1324. return -ENOMEM;
  1325. }
  1326. drv_data = spi_master_get_devdata(master);
  1327. drv_data->master = master;
  1328. drv_data->master_info = platform_info;
  1329. drv_data->pdev = pdev;
  1330. drv_data->ssp = ssp;
  1331. master->dev.of_node = pdev->dev.of_node;
  1332. /* the spi->mode bits understood by this driver: */
  1333. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1334. master->bus_num = ssp->port_id;
  1335. master->dma_alignment = DMA_ALIGNMENT;
  1336. master->cleanup = cleanup;
  1337. master->setup = setup;
  1338. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1339. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1340. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1341. master->auto_runtime_pm = true;
  1342. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  1343. drv_data->ssp_type = ssp->type;
  1344. drv_data->ioaddr = ssp->mmio_base;
  1345. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1346. if (pxa25x_ssp_comp(drv_data)) {
  1347. switch (drv_data->ssp_type) {
  1348. case QUARK_X1000_SSP:
  1349. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1350. break;
  1351. default:
  1352. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1353. break;
  1354. }
  1355. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1356. drv_data->dma_cr1 = 0;
  1357. drv_data->clear_sr = SSSR_ROR;
  1358. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1359. } else {
  1360. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1361. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1362. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1363. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1364. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1365. }
  1366. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1367. drv_data);
  1368. if (status < 0) {
  1369. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1370. goto out_error_master_alloc;
  1371. }
  1372. /* Setup DMA if requested */
  1373. if (platform_info->enable_dma) {
  1374. status = pxa2xx_spi_dma_setup(drv_data);
  1375. if (status) {
  1376. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1377. platform_info->enable_dma = false;
  1378. } else {
  1379. master->can_dma = pxa2xx_spi_can_dma;
  1380. }
  1381. }
  1382. /* Enable SOC clock */
  1383. clk_prepare_enable(ssp->clk);
  1384. master->max_speed_hz = clk_get_rate(ssp->clk);
  1385. /* Load default SSP configuration */
  1386. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1387. switch (drv_data->ssp_type) {
  1388. case QUARK_X1000_SSP:
  1389. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
  1390. | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1391. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1392. /* using the Motorola SPI protocol and use 8 bit frame */
  1393. pxa2xx_spi_write(drv_data, SSCR0,
  1394. QUARK_X1000_SSCR0_Motorola
  1395. | QUARK_X1000_SSCR0_DataSize(8));
  1396. break;
  1397. default:
  1398. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1399. SSCR1_TxTresh(TX_THRESH_DFLT);
  1400. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1401. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1402. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1403. break;
  1404. }
  1405. if (!pxa25x_ssp_comp(drv_data))
  1406. pxa2xx_spi_write(drv_data, SSTO, 0);
  1407. if (!is_quark_x1000_ssp(drv_data))
  1408. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1409. if (is_lpss_ssp(drv_data)) {
  1410. lpss_ssp_setup(drv_data);
  1411. config = lpss_get_config(drv_data);
  1412. if (config->reg_capabilities >= 0) {
  1413. tmp = __lpss_ssp_read_priv(drv_data,
  1414. config->reg_capabilities);
  1415. tmp &= LPSS_CAPS_CS_EN_MASK;
  1416. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1417. platform_info->num_chipselect = ffz(tmp);
  1418. } else if (config->cs_num) {
  1419. platform_info->num_chipselect = config->cs_num;
  1420. }
  1421. }
  1422. master->num_chipselect = platform_info->num_chipselect;
  1423. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1424. (unsigned long)drv_data);
  1425. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1426. pm_runtime_use_autosuspend(&pdev->dev);
  1427. pm_runtime_set_active(&pdev->dev);
  1428. pm_runtime_enable(&pdev->dev);
  1429. /* Register with the SPI framework */
  1430. platform_set_drvdata(pdev, drv_data);
  1431. status = devm_spi_register_master(&pdev->dev, master);
  1432. if (status != 0) {
  1433. dev_err(&pdev->dev, "problem registering spi master\n");
  1434. goto out_error_clock_enabled;
  1435. }
  1436. return status;
  1437. out_error_clock_enabled:
  1438. clk_disable_unprepare(ssp->clk);
  1439. pxa2xx_spi_dma_release(drv_data);
  1440. free_irq(ssp->irq, drv_data);
  1441. out_error_master_alloc:
  1442. spi_master_put(master);
  1443. pxa_ssp_free(ssp);
  1444. return status;
  1445. }
  1446. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1447. {
  1448. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1449. struct ssp_device *ssp;
  1450. if (!drv_data)
  1451. return 0;
  1452. ssp = drv_data->ssp;
  1453. pm_runtime_get_sync(&pdev->dev);
  1454. /* Disable the SSP at the peripheral and SOC level */
  1455. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1456. clk_disable_unprepare(ssp->clk);
  1457. /* Release DMA */
  1458. if (drv_data->master_info->enable_dma)
  1459. pxa2xx_spi_dma_release(drv_data);
  1460. pm_runtime_put_noidle(&pdev->dev);
  1461. pm_runtime_disable(&pdev->dev);
  1462. /* Release IRQ */
  1463. free_irq(ssp->irq, drv_data);
  1464. /* Release SSP */
  1465. pxa_ssp_free(ssp);
  1466. return 0;
  1467. }
  1468. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1469. {
  1470. int status = 0;
  1471. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1472. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1473. }
  1474. #ifdef CONFIG_PM_SLEEP
  1475. static int pxa2xx_spi_suspend(struct device *dev)
  1476. {
  1477. struct driver_data *drv_data = dev_get_drvdata(dev);
  1478. struct ssp_device *ssp = drv_data->ssp;
  1479. int status = 0;
  1480. status = spi_master_suspend(drv_data->master);
  1481. if (status != 0)
  1482. return status;
  1483. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1484. if (!pm_runtime_suspended(dev))
  1485. clk_disable_unprepare(ssp->clk);
  1486. return 0;
  1487. }
  1488. static int pxa2xx_spi_resume(struct device *dev)
  1489. {
  1490. struct driver_data *drv_data = dev_get_drvdata(dev);
  1491. struct ssp_device *ssp = drv_data->ssp;
  1492. int status = 0;
  1493. /* Enable the SSP clock */
  1494. if (!pm_runtime_suspended(dev))
  1495. clk_prepare_enable(ssp->clk);
  1496. /* Restore LPSS private register bits */
  1497. if (is_lpss_ssp(drv_data))
  1498. lpss_ssp_setup(drv_data);
  1499. /* Start the queue running */
  1500. status = spi_master_resume(drv_data->master);
  1501. if (status != 0) {
  1502. dev_err(dev, "problem starting queue (%d)\n", status);
  1503. return status;
  1504. }
  1505. return 0;
  1506. }
  1507. #endif
  1508. #ifdef CONFIG_PM
  1509. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1510. {
  1511. struct driver_data *drv_data = dev_get_drvdata(dev);
  1512. clk_disable_unprepare(drv_data->ssp->clk);
  1513. return 0;
  1514. }
  1515. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1516. {
  1517. struct driver_data *drv_data = dev_get_drvdata(dev);
  1518. clk_prepare_enable(drv_data->ssp->clk);
  1519. return 0;
  1520. }
  1521. #endif
  1522. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1523. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1524. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1525. pxa2xx_spi_runtime_resume, NULL)
  1526. };
  1527. static struct platform_driver driver = {
  1528. .driver = {
  1529. .name = "pxa2xx-spi",
  1530. .pm = &pxa2xx_spi_pm_ops,
  1531. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1532. },
  1533. .probe = pxa2xx_spi_probe,
  1534. .remove = pxa2xx_spi_remove,
  1535. .shutdown = pxa2xx_spi_shutdown,
  1536. };
  1537. static int __init pxa2xx_spi_init(void)
  1538. {
  1539. return platform_driver_register(&driver);
  1540. }
  1541. subsys_initcall(pxa2xx_spi_init);
  1542. static void __exit pxa2xx_spi_exit(void)
  1543. {
  1544. platform_driver_unregister(&driver);
  1545. }
  1546. module_exit(pxa2xx_spi_exit);