spi-pxa2xx.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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 = 0xfc,
  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. next_msg = NULL;
  493. if (!next_msg || msg->state == ERROR_STATE)
  494. cs_deassert(drv_data);
  495. }
  496. drv_data->cur_chip = NULL;
  497. spi_finalize_current_message(drv_data->master);
  498. }
  499. static void reset_sccr1(struct driver_data *drv_data)
  500. {
  501. struct chip_data *chip = drv_data->cur_chip;
  502. u32 sccr1_reg;
  503. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  504. sccr1_reg &= ~SSCR1_RFT;
  505. sccr1_reg |= chip->threshold;
  506. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  507. }
  508. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  509. {
  510. /* Stop and reset SSP */
  511. write_SSSR_CS(drv_data, drv_data->clear_sr);
  512. reset_sccr1(drv_data);
  513. if (!pxa25x_ssp_comp(drv_data))
  514. pxa2xx_spi_write(drv_data, SSTO, 0);
  515. pxa2xx_spi_flush(drv_data);
  516. pxa2xx_spi_write(drv_data, SSCR0,
  517. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  518. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  519. drv_data->cur_msg->state = ERROR_STATE;
  520. tasklet_schedule(&drv_data->pump_transfers);
  521. }
  522. static void int_transfer_complete(struct driver_data *drv_data)
  523. {
  524. /* Clear and disable interrupts */
  525. write_SSSR_CS(drv_data, drv_data->clear_sr);
  526. reset_sccr1(drv_data);
  527. if (!pxa25x_ssp_comp(drv_data))
  528. pxa2xx_spi_write(drv_data, SSTO, 0);
  529. /* Update total byte transferred return count actual bytes read */
  530. drv_data->cur_msg->actual_length += drv_data->len -
  531. (drv_data->rx_end - drv_data->rx);
  532. /* Transfer delays and chip select release are
  533. * handled in pump_transfers or giveback
  534. */
  535. /* Move to next transfer */
  536. drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  537. /* Schedule transfer tasklet */
  538. tasklet_schedule(&drv_data->pump_transfers);
  539. }
  540. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  541. {
  542. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  543. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  544. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  545. if (irq_status & SSSR_ROR) {
  546. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  547. return IRQ_HANDLED;
  548. }
  549. if (irq_status & SSSR_TINT) {
  550. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  551. if (drv_data->read(drv_data)) {
  552. int_transfer_complete(drv_data);
  553. return IRQ_HANDLED;
  554. }
  555. }
  556. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  557. do {
  558. if (drv_data->read(drv_data)) {
  559. int_transfer_complete(drv_data);
  560. return IRQ_HANDLED;
  561. }
  562. } while (drv_data->write(drv_data));
  563. if (drv_data->read(drv_data)) {
  564. int_transfer_complete(drv_data);
  565. return IRQ_HANDLED;
  566. }
  567. if (drv_data->tx == drv_data->tx_end) {
  568. u32 bytes_left;
  569. u32 sccr1_reg;
  570. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  571. sccr1_reg &= ~SSCR1_TIE;
  572. /*
  573. * PXA25x_SSP has no timeout, set up rx threshould for the
  574. * remaining RX bytes.
  575. */
  576. if (pxa25x_ssp_comp(drv_data)) {
  577. u32 rx_thre;
  578. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  579. bytes_left = drv_data->rx_end - drv_data->rx;
  580. switch (drv_data->n_bytes) {
  581. case 4:
  582. bytes_left >>= 1;
  583. case 2:
  584. bytes_left >>= 1;
  585. }
  586. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  587. if (rx_thre > bytes_left)
  588. rx_thre = bytes_left;
  589. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  590. }
  591. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  592. }
  593. /* We did something */
  594. return IRQ_HANDLED;
  595. }
  596. static irqreturn_t ssp_int(int irq, void *dev_id)
  597. {
  598. struct driver_data *drv_data = dev_id;
  599. u32 sccr1_reg;
  600. u32 mask = drv_data->mask_sr;
  601. u32 status;
  602. /*
  603. * The IRQ might be shared with other peripherals so we must first
  604. * check that are we RPM suspended or not. If we are we assume that
  605. * the IRQ was not for us (we shouldn't be RPM suspended when the
  606. * interrupt is enabled).
  607. */
  608. if (pm_runtime_suspended(&drv_data->pdev->dev))
  609. return IRQ_NONE;
  610. /*
  611. * If the device is not yet in RPM suspended state and we get an
  612. * interrupt that is meant for another device, check if status bits
  613. * are all set to one. That means that the device is already
  614. * powered off.
  615. */
  616. status = pxa2xx_spi_read(drv_data, SSSR);
  617. if (status == ~0)
  618. return IRQ_NONE;
  619. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  620. /* Ignore possible writes if we don't need to write */
  621. if (!(sccr1_reg & SSCR1_TIE))
  622. mask &= ~SSSR_TFS;
  623. /* Ignore RX timeout interrupt if it is disabled */
  624. if (!(sccr1_reg & SSCR1_TINTE))
  625. mask &= ~SSSR_TINT;
  626. if (!(status & mask))
  627. return IRQ_NONE;
  628. if (!drv_data->cur_msg) {
  629. pxa2xx_spi_write(drv_data, SSCR0,
  630. pxa2xx_spi_read(drv_data, SSCR0)
  631. & ~SSCR0_SSE);
  632. pxa2xx_spi_write(drv_data, SSCR1,
  633. pxa2xx_spi_read(drv_data, SSCR1)
  634. & ~drv_data->int_cr1);
  635. if (!pxa25x_ssp_comp(drv_data))
  636. pxa2xx_spi_write(drv_data, SSTO, 0);
  637. write_SSSR_CS(drv_data, drv_data->clear_sr);
  638. dev_err(&drv_data->pdev->dev,
  639. "bad message state in interrupt handler\n");
  640. /* Never fail */
  641. return IRQ_HANDLED;
  642. }
  643. return drv_data->transfer_handler(drv_data);
  644. }
  645. /*
  646. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  647. * input frequency by fractions of 2^24. It also has a divider by 5.
  648. *
  649. * There are formulas to get baud rate value for given input frequency and
  650. * divider parameters, such as DDS_CLK_RATE and SCR:
  651. *
  652. * Fsys = 200MHz
  653. *
  654. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  655. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  656. *
  657. * DDS_CLK_RATE either 2^n or 2^n / 5.
  658. * SCR is in range 0 .. 255
  659. *
  660. * Divisor = 5^i * 2^j * 2 * k
  661. * i = [0, 1] i = 1 iff j = 0 or j > 3
  662. * j = [0, 23] j = 0 iff i = 1
  663. * k = [1, 256]
  664. * Special case: j = 0, i = 1: Divisor = 2 / 5
  665. *
  666. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  667. * are:
  668. * Case 1: 2^n, n = [0, 23]
  669. * Case 2: 2^24 * 2 / 5 (0x666666)
  670. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  671. *
  672. * In all cases the lowest possible value is better.
  673. *
  674. * The function calculates parameters for all cases and chooses the one closest
  675. * to the asked baud rate.
  676. */
  677. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  678. {
  679. unsigned long xtal = 200000000;
  680. unsigned long fref = xtal / 2; /* mandatory division by 2,
  681. see (2) */
  682. /* case 3 */
  683. unsigned long fref1 = fref / 2; /* case 1 */
  684. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  685. unsigned long scale;
  686. unsigned long q, q1, q2;
  687. long r, r1, r2;
  688. u32 mul;
  689. /* Case 1 */
  690. /* Set initial value for DDS_CLK_RATE */
  691. mul = (1 << 24) >> 1;
  692. /* Calculate initial quot */
  693. q1 = DIV_ROUND_UP(fref1, rate);
  694. /* Scale q1 if it's too big */
  695. if (q1 > 256) {
  696. /* Scale q1 to range [1, 512] */
  697. scale = fls_long(q1 - 1);
  698. if (scale > 9) {
  699. q1 >>= scale - 9;
  700. mul >>= scale - 9;
  701. }
  702. /* Round the result if we have a remainder */
  703. q1 += q1 & 1;
  704. }
  705. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  706. scale = __ffs(q1);
  707. q1 >>= scale;
  708. mul >>= scale;
  709. /* Get the remainder */
  710. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  711. /* Case 2 */
  712. q2 = DIV_ROUND_UP(fref2, rate);
  713. r2 = abs(fref2 / q2 - rate);
  714. /*
  715. * Choose the best between two: less remainder we have the better. We
  716. * can't go case 2 if q2 is greater than 256 since SCR register can
  717. * hold only values 0 .. 255.
  718. */
  719. if (r2 >= r1 || q2 > 256) {
  720. /* case 1 is better */
  721. r = r1;
  722. q = q1;
  723. } else {
  724. /* case 2 is better */
  725. r = r2;
  726. q = q2;
  727. mul = (1 << 24) * 2 / 5;
  728. }
  729. /* Check case 3 only if the divisor is big enough */
  730. if (fref / rate >= 80) {
  731. u64 fssp;
  732. u32 m;
  733. /* Calculate initial quot */
  734. q1 = DIV_ROUND_UP(fref, rate);
  735. m = (1 << 24) / q1;
  736. /* Get the remainder */
  737. fssp = (u64)fref * m;
  738. do_div(fssp, 1 << 24);
  739. r1 = abs(fssp - rate);
  740. /* Choose this one if it suits better */
  741. if (r1 < r) {
  742. /* case 3 is better */
  743. q = 1;
  744. mul = m;
  745. }
  746. }
  747. *dds = mul;
  748. return q - 1;
  749. }
  750. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  751. {
  752. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  753. const struct ssp_device *ssp = drv_data->ssp;
  754. rate = min_t(int, ssp_clk, rate);
  755. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  756. return (ssp_clk / (2 * rate) - 1) & 0xff;
  757. else
  758. return (ssp_clk / rate - 1) & 0xfff;
  759. }
  760. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  761. int rate)
  762. {
  763. struct chip_data *chip = drv_data->cur_chip;
  764. unsigned int clk_div;
  765. switch (drv_data->ssp_type) {
  766. case QUARK_X1000_SSP:
  767. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  768. break;
  769. default:
  770. clk_div = ssp_get_clk_div(drv_data, rate);
  771. break;
  772. }
  773. return clk_div << 8;
  774. }
  775. static void pump_transfers(unsigned long data)
  776. {
  777. struct driver_data *drv_data = (struct driver_data *)data;
  778. struct spi_message *message = NULL;
  779. struct spi_transfer *transfer = NULL;
  780. struct spi_transfer *previous = NULL;
  781. struct chip_data *chip = NULL;
  782. u32 clk_div = 0;
  783. u8 bits = 0;
  784. u32 speed = 0;
  785. u32 cr0;
  786. u32 cr1;
  787. u32 dma_thresh = drv_data->cur_chip->dma_threshold;
  788. u32 dma_burst = drv_data->cur_chip->dma_burst_size;
  789. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  790. /* Get current state information */
  791. message = drv_data->cur_msg;
  792. transfer = drv_data->cur_transfer;
  793. chip = drv_data->cur_chip;
  794. /* Handle for abort */
  795. if (message->state == ERROR_STATE) {
  796. message->status = -EIO;
  797. giveback(drv_data);
  798. return;
  799. }
  800. /* Handle end of message */
  801. if (message->state == DONE_STATE) {
  802. message->status = 0;
  803. giveback(drv_data);
  804. return;
  805. }
  806. /* Delay if requested at end of transfer before CS change */
  807. if (message->state == RUNNING_STATE) {
  808. previous = list_entry(transfer->transfer_list.prev,
  809. struct spi_transfer,
  810. transfer_list);
  811. if (previous->delay_usecs)
  812. udelay(previous->delay_usecs);
  813. /* Drop chip select only if cs_change is requested */
  814. if (previous->cs_change)
  815. cs_deassert(drv_data);
  816. }
  817. /* Check if we can DMA this transfer */
  818. if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
  819. /* reject already-mapped transfers; PIO won't always work */
  820. if (message->is_dma_mapped
  821. || transfer->rx_dma || transfer->tx_dma) {
  822. dev_err(&drv_data->pdev->dev,
  823. "pump_transfers: mapped transfer length of "
  824. "%u is greater than %d\n",
  825. transfer->len, MAX_DMA_LEN);
  826. message->status = -EINVAL;
  827. giveback(drv_data);
  828. return;
  829. }
  830. /* warn ... we force this to PIO mode */
  831. dev_warn_ratelimited(&message->spi->dev,
  832. "pump_transfers: DMA disabled for transfer length %ld "
  833. "greater than %d\n",
  834. (long)drv_data->len, MAX_DMA_LEN);
  835. }
  836. /* Setup the transfer state based on the type of transfer */
  837. if (pxa2xx_spi_flush(drv_data) == 0) {
  838. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  839. message->status = -EIO;
  840. giveback(drv_data);
  841. return;
  842. }
  843. drv_data->n_bytes = chip->n_bytes;
  844. drv_data->tx = (void *)transfer->tx_buf;
  845. drv_data->tx_end = drv_data->tx + transfer->len;
  846. drv_data->rx = transfer->rx_buf;
  847. drv_data->rx_end = drv_data->rx + transfer->len;
  848. drv_data->len = transfer->len;
  849. drv_data->write = drv_data->tx ? chip->write : null_writer;
  850. drv_data->read = drv_data->rx ? chip->read : null_reader;
  851. /* Change speed and bit per word on a per transfer */
  852. bits = transfer->bits_per_word;
  853. speed = transfer->speed_hz;
  854. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  855. if (bits <= 8) {
  856. drv_data->n_bytes = 1;
  857. drv_data->read = drv_data->read != null_reader ?
  858. u8_reader : null_reader;
  859. drv_data->write = drv_data->write != null_writer ?
  860. u8_writer : null_writer;
  861. } else if (bits <= 16) {
  862. drv_data->n_bytes = 2;
  863. drv_data->read = drv_data->read != null_reader ?
  864. u16_reader : null_reader;
  865. drv_data->write = drv_data->write != null_writer ?
  866. u16_writer : null_writer;
  867. } else if (bits <= 32) {
  868. drv_data->n_bytes = 4;
  869. drv_data->read = drv_data->read != null_reader ?
  870. u32_reader : null_reader;
  871. drv_data->write = drv_data->write != null_writer ?
  872. u32_writer : null_writer;
  873. }
  874. /*
  875. * if bits/word is changed in dma mode, then must check the
  876. * thresholds and burst also
  877. */
  878. if (chip->enable_dma) {
  879. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  880. message->spi,
  881. bits, &dma_burst,
  882. &dma_thresh))
  883. dev_warn_ratelimited(&message->spi->dev,
  884. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  885. }
  886. message->state = RUNNING_STATE;
  887. drv_data->dma_mapped = 0;
  888. if (pxa2xx_spi_dma_is_possible(drv_data->len))
  889. drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
  890. if (drv_data->dma_mapped) {
  891. /* Ensure we have the correct interrupt handler */
  892. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  893. pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  894. /* Clear status and start DMA engine */
  895. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  896. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  897. pxa2xx_spi_dma_start(drv_data);
  898. } else {
  899. /* Ensure we have the correct interrupt handler */
  900. drv_data->transfer_handler = interrupt_transfer;
  901. /* Clear status */
  902. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  903. write_SSSR_CS(drv_data, drv_data->clear_sr);
  904. }
  905. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  906. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  907. if (!pxa25x_ssp_comp(drv_data))
  908. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  909. drv_data->master->max_speed_hz
  910. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  911. drv_data->dma_mapped ? "DMA" : "PIO");
  912. else
  913. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  914. drv_data->master->max_speed_hz / 2
  915. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  916. drv_data->dma_mapped ? "DMA" : "PIO");
  917. if (is_lpss_ssp(drv_data)) {
  918. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  919. != chip->lpss_rx_threshold)
  920. pxa2xx_spi_write(drv_data, SSIRF,
  921. chip->lpss_rx_threshold);
  922. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  923. != chip->lpss_tx_threshold)
  924. pxa2xx_spi_write(drv_data, SSITF,
  925. chip->lpss_tx_threshold);
  926. }
  927. if (is_quark_x1000_ssp(drv_data) &&
  928. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  929. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  930. /* see if we need to reload the config registers */
  931. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  932. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  933. != (cr1 & change_mask)) {
  934. /* stop the SSP, and update the other bits */
  935. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  936. if (!pxa25x_ssp_comp(drv_data))
  937. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  938. /* first set CR1 without interrupt and service enables */
  939. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  940. /* restart the SSP */
  941. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  942. } else {
  943. if (!pxa25x_ssp_comp(drv_data))
  944. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  945. }
  946. cs_assert(drv_data);
  947. /* after chip select, release the data by enabling service
  948. * requests and interrupts, without changing any mode bits */
  949. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  950. }
  951. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  952. struct spi_message *msg)
  953. {
  954. struct driver_data *drv_data = spi_master_get_devdata(master);
  955. drv_data->cur_msg = msg;
  956. /* Initial message state*/
  957. drv_data->cur_msg->state = START_STATE;
  958. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  959. struct spi_transfer,
  960. transfer_list);
  961. /* prepare to setup the SSP, in pump_transfers, using the per
  962. * chip configuration */
  963. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  964. /* Mark as busy and launch transfers */
  965. tasklet_schedule(&drv_data->pump_transfers);
  966. return 0;
  967. }
  968. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  969. {
  970. struct driver_data *drv_data = spi_master_get_devdata(master);
  971. /* Disable the SSP now */
  972. pxa2xx_spi_write(drv_data, SSCR0,
  973. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  974. return 0;
  975. }
  976. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  977. struct pxa2xx_spi_chip *chip_info)
  978. {
  979. int err = 0;
  980. if (chip == NULL || chip_info == NULL)
  981. return 0;
  982. /* NOTE: setup() can be called multiple times, possibly with
  983. * different chip_info, release previously requested GPIO
  984. */
  985. if (gpio_is_valid(chip->gpio_cs))
  986. gpio_free(chip->gpio_cs);
  987. /* If (*cs_control) is provided, ignore GPIO chip select */
  988. if (chip_info->cs_control) {
  989. chip->cs_control = chip_info->cs_control;
  990. return 0;
  991. }
  992. if (gpio_is_valid(chip_info->gpio_cs)) {
  993. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  994. if (err) {
  995. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  996. chip_info->gpio_cs);
  997. return err;
  998. }
  999. chip->gpio_cs = chip_info->gpio_cs;
  1000. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1001. err = gpio_direction_output(chip->gpio_cs,
  1002. !chip->gpio_cs_inverted);
  1003. }
  1004. return err;
  1005. }
  1006. static int setup(struct spi_device *spi)
  1007. {
  1008. struct pxa2xx_spi_chip *chip_info = NULL;
  1009. struct chip_data *chip;
  1010. const struct lpss_config *config;
  1011. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1012. uint tx_thres, tx_hi_thres, rx_thres;
  1013. switch (drv_data->ssp_type) {
  1014. case QUARK_X1000_SSP:
  1015. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1016. tx_hi_thres = 0;
  1017. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1018. break;
  1019. case LPSS_LPT_SSP:
  1020. case LPSS_BYT_SSP:
  1021. case LPSS_BSW_SSP:
  1022. case LPSS_SPT_SSP:
  1023. case LPSS_BXT_SSP:
  1024. config = lpss_get_config(drv_data);
  1025. tx_thres = config->tx_threshold_lo;
  1026. tx_hi_thres = config->tx_threshold_hi;
  1027. rx_thres = config->rx_threshold;
  1028. break;
  1029. default:
  1030. tx_thres = TX_THRESH_DFLT;
  1031. tx_hi_thres = 0;
  1032. rx_thres = RX_THRESH_DFLT;
  1033. break;
  1034. }
  1035. /* Only alloc on first setup */
  1036. chip = spi_get_ctldata(spi);
  1037. if (!chip) {
  1038. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1039. if (!chip)
  1040. return -ENOMEM;
  1041. if (drv_data->ssp_type == CE4100_SSP) {
  1042. if (spi->chip_select > 4) {
  1043. dev_err(&spi->dev,
  1044. "failed setup: cs number must not be > 4.\n");
  1045. kfree(chip);
  1046. return -EINVAL;
  1047. }
  1048. chip->frm = spi->chip_select;
  1049. } else
  1050. chip->gpio_cs = -1;
  1051. chip->enable_dma = 0;
  1052. chip->timeout = TIMOUT_DFLT;
  1053. }
  1054. /* protocol drivers may change the chip settings, so...
  1055. * if chip_info exists, use it */
  1056. chip_info = spi->controller_data;
  1057. /* chip_info isn't always needed */
  1058. chip->cr1 = 0;
  1059. if (chip_info) {
  1060. if (chip_info->timeout)
  1061. chip->timeout = chip_info->timeout;
  1062. if (chip_info->tx_threshold)
  1063. tx_thres = chip_info->tx_threshold;
  1064. if (chip_info->tx_hi_threshold)
  1065. tx_hi_thres = chip_info->tx_hi_threshold;
  1066. if (chip_info->rx_threshold)
  1067. rx_thres = chip_info->rx_threshold;
  1068. chip->enable_dma = drv_data->master_info->enable_dma;
  1069. chip->dma_threshold = 0;
  1070. if (chip_info->enable_loopback)
  1071. chip->cr1 = SSCR1_LBM;
  1072. } else if (ACPI_HANDLE(&spi->dev)) {
  1073. /*
  1074. * Slave devices enumerated from ACPI namespace don't
  1075. * usually have chip_info but we still might want to use
  1076. * DMA with them.
  1077. */
  1078. chip->enable_dma = drv_data->master_info->enable_dma;
  1079. }
  1080. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1081. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1082. | SSITF_TxHiThresh(tx_hi_thres);
  1083. /* set dma burst and threshold outside of chip_info path so that if
  1084. * chip_info goes away after setting chip->enable_dma, the
  1085. * burst and threshold can still respond to changes in bits_per_word */
  1086. if (chip->enable_dma) {
  1087. /* set up legal burst and threshold for dma */
  1088. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1089. spi->bits_per_word,
  1090. &chip->dma_burst_size,
  1091. &chip->dma_threshold)) {
  1092. dev_warn(&spi->dev,
  1093. "in setup: DMA burst size reduced to match bits_per_word\n");
  1094. }
  1095. }
  1096. switch (drv_data->ssp_type) {
  1097. case QUARK_X1000_SSP:
  1098. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1099. & QUARK_X1000_SSCR1_RFT)
  1100. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1101. & QUARK_X1000_SSCR1_TFT);
  1102. break;
  1103. default:
  1104. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1105. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1106. break;
  1107. }
  1108. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1109. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1110. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1111. if (spi->mode & SPI_LOOP)
  1112. chip->cr1 |= SSCR1_LBM;
  1113. if (spi->bits_per_word <= 8) {
  1114. chip->n_bytes = 1;
  1115. chip->read = u8_reader;
  1116. chip->write = u8_writer;
  1117. } else if (spi->bits_per_word <= 16) {
  1118. chip->n_bytes = 2;
  1119. chip->read = u16_reader;
  1120. chip->write = u16_writer;
  1121. } else if (spi->bits_per_word <= 32) {
  1122. chip->n_bytes = 4;
  1123. chip->read = u32_reader;
  1124. chip->write = u32_writer;
  1125. }
  1126. spi_set_ctldata(spi, chip);
  1127. if (drv_data->ssp_type == CE4100_SSP)
  1128. return 0;
  1129. return setup_cs(spi, chip, chip_info);
  1130. }
  1131. static void cleanup(struct spi_device *spi)
  1132. {
  1133. struct chip_data *chip = spi_get_ctldata(spi);
  1134. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1135. if (!chip)
  1136. return;
  1137. if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
  1138. gpio_free(chip->gpio_cs);
  1139. kfree(chip);
  1140. }
  1141. #ifdef CONFIG_PCI
  1142. #ifdef CONFIG_ACPI
  1143. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1144. { "INT33C0", LPSS_LPT_SSP },
  1145. { "INT33C1", LPSS_LPT_SSP },
  1146. { "INT3430", LPSS_LPT_SSP },
  1147. { "INT3431", LPSS_LPT_SSP },
  1148. { "80860F0E", LPSS_BYT_SSP },
  1149. { "8086228E", LPSS_BSW_SSP },
  1150. { },
  1151. };
  1152. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1153. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1154. {
  1155. unsigned int devid;
  1156. int port_id = -1;
  1157. if (adev && adev->pnp.unique_id &&
  1158. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1159. port_id = devid;
  1160. return port_id;
  1161. }
  1162. #else /* !CONFIG_ACPI */
  1163. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1164. {
  1165. return -1;
  1166. }
  1167. #endif
  1168. /*
  1169. * PCI IDs of compound devices that integrate both host controller and private
  1170. * integrated DMA engine. Please note these are not used in module
  1171. * autoloading and probing in this module but matching the LPSS SSP type.
  1172. */
  1173. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1174. /* SPT-LP */
  1175. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1176. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1177. /* SPT-H */
  1178. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1179. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1180. /* BXT A-Step */
  1181. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1182. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1183. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1184. /* BXT B-Step */
  1185. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1186. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1187. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1188. /* APL */
  1189. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1190. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1191. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1192. { },
  1193. };
  1194. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1195. {
  1196. struct device *dev = param;
  1197. if (dev != chan->device->dev->parent)
  1198. return false;
  1199. return true;
  1200. }
  1201. static struct pxa2xx_spi_master *
  1202. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1203. {
  1204. struct pxa2xx_spi_master *pdata;
  1205. struct acpi_device *adev;
  1206. struct ssp_device *ssp;
  1207. struct resource *res;
  1208. const struct acpi_device_id *adev_id = NULL;
  1209. const struct pci_device_id *pcidev_id = NULL;
  1210. int type;
  1211. adev = ACPI_COMPANION(&pdev->dev);
  1212. if (dev_is_pci(pdev->dev.parent))
  1213. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1214. to_pci_dev(pdev->dev.parent));
  1215. else if (adev)
  1216. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1217. &pdev->dev);
  1218. else
  1219. return NULL;
  1220. if (adev_id)
  1221. type = (int)adev_id->driver_data;
  1222. else if (pcidev_id)
  1223. type = (int)pcidev_id->driver_data;
  1224. else
  1225. return NULL;
  1226. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1227. if (!pdata)
  1228. return NULL;
  1229. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1230. if (!res)
  1231. return NULL;
  1232. ssp = &pdata->ssp;
  1233. ssp->phys_base = res->start;
  1234. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1235. if (IS_ERR(ssp->mmio_base))
  1236. return NULL;
  1237. if (pcidev_id) {
  1238. pdata->tx_param = pdev->dev.parent;
  1239. pdata->rx_param = pdev->dev.parent;
  1240. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1241. }
  1242. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1243. ssp->irq = platform_get_irq(pdev, 0);
  1244. ssp->type = type;
  1245. ssp->pdev = pdev;
  1246. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1247. pdata->num_chipselect = 1;
  1248. pdata->enable_dma = true;
  1249. return pdata;
  1250. }
  1251. #else /* !CONFIG_PCI */
  1252. static inline struct pxa2xx_spi_master *
  1253. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1254. {
  1255. return NULL;
  1256. }
  1257. #endif
  1258. static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
  1259. {
  1260. struct driver_data *drv_data = spi_master_get_devdata(master);
  1261. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1262. switch (drv_data->ssp_type) {
  1263. /*
  1264. * For Atoms the ACPI DeviceSelection used by the Windows
  1265. * driver starts from 1 instead of 0 so translate it here
  1266. * to match what Linux expects.
  1267. */
  1268. case LPSS_BYT_SSP:
  1269. case LPSS_BSW_SSP:
  1270. return cs - 1;
  1271. default:
  1272. break;
  1273. }
  1274. }
  1275. return cs;
  1276. }
  1277. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1278. {
  1279. struct device *dev = &pdev->dev;
  1280. struct pxa2xx_spi_master *platform_info;
  1281. struct spi_master *master;
  1282. struct driver_data *drv_data;
  1283. struct ssp_device *ssp;
  1284. const struct lpss_config *config;
  1285. int status;
  1286. u32 tmp;
  1287. platform_info = dev_get_platdata(dev);
  1288. if (!platform_info) {
  1289. platform_info = pxa2xx_spi_init_pdata(pdev);
  1290. if (!platform_info) {
  1291. dev_err(&pdev->dev, "missing platform data\n");
  1292. return -ENODEV;
  1293. }
  1294. }
  1295. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1296. if (!ssp)
  1297. ssp = &platform_info->ssp;
  1298. if (!ssp->mmio_base) {
  1299. dev_err(&pdev->dev, "failed to get ssp\n");
  1300. return -ENODEV;
  1301. }
  1302. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1303. if (!master) {
  1304. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1305. pxa_ssp_free(ssp);
  1306. return -ENOMEM;
  1307. }
  1308. drv_data = spi_master_get_devdata(master);
  1309. drv_data->master = master;
  1310. drv_data->master_info = platform_info;
  1311. drv_data->pdev = pdev;
  1312. drv_data->ssp = ssp;
  1313. master->dev.parent = &pdev->dev;
  1314. master->dev.of_node = pdev->dev.of_node;
  1315. /* the spi->mode bits understood by this driver: */
  1316. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1317. master->bus_num = ssp->port_id;
  1318. master->dma_alignment = DMA_ALIGNMENT;
  1319. master->cleanup = cleanup;
  1320. master->setup = setup;
  1321. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1322. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1323. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1324. master->auto_runtime_pm = true;
  1325. drv_data->ssp_type = ssp->type;
  1326. drv_data->ioaddr = ssp->mmio_base;
  1327. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1328. if (pxa25x_ssp_comp(drv_data)) {
  1329. switch (drv_data->ssp_type) {
  1330. case QUARK_X1000_SSP:
  1331. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1332. break;
  1333. default:
  1334. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1335. break;
  1336. }
  1337. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1338. drv_data->dma_cr1 = 0;
  1339. drv_data->clear_sr = SSSR_ROR;
  1340. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1341. } else {
  1342. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1343. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1344. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1345. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1346. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1347. }
  1348. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1349. drv_data);
  1350. if (status < 0) {
  1351. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1352. goto out_error_master_alloc;
  1353. }
  1354. /* Setup DMA if requested */
  1355. if (platform_info->enable_dma) {
  1356. status = pxa2xx_spi_dma_setup(drv_data);
  1357. if (status) {
  1358. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1359. platform_info->enable_dma = false;
  1360. }
  1361. }
  1362. /* Enable SOC clock */
  1363. clk_prepare_enable(ssp->clk);
  1364. master->max_speed_hz = clk_get_rate(ssp->clk);
  1365. /* Load default SSP configuration */
  1366. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1367. switch (drv_data->ssp_type) {
  1368. case QUARK_X1000_SSP:
  1369. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
  1370. | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1371. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1372. /* using the Motorola SPI protocol and use 8 bit frame */
  1373. pxa2xx_spi_write(drv_data, SSCR0,
  1374. QUARK_X1000_SSCR0_Motorola
  1375. | QUARK_X1000_SSCR0_DataSize(8));
  1376. break;
  1377. default:
  1378. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1379. SSCR1_TxTresh(TX_THRESH_DFLT);
  1380. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1381. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1382. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1383. break;
  1384. }
  1385. if (!pxa25x_ssp_comp(drv_data))
  1386. pxa2xx_spi_write(drv_data, SSTO, 0);
  1387. if (!is_quark_x1000_ssp(drv_data))
  1388. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1389. if (is_lpss_ssp(drv_data)) {
  1390. lpss_ssp_setup(drv_data);
  1391. config = lpss_get_config(drv_data);
  1392. if (config->reg_capabilities >= 0) {
  1393. tmp = __lpss_ssp_read_priv(drv_data,
  1394. config->reg_capabilities);
  1395. tmp &= LPSS_CAPS_CS_EN_MASK;
  1396. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1397. platform_info->num_chipselect = ffz(tmp);
  1398. } else if (config->cs_num) {
  1399. platform_info->num_chipselect = config->cs_num;
  1400. }
  1401. }
  1402. master->num_chipselect = platform_info->num_chipselect;
  1403. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1404. (unsigned long)drv_data);
  1405. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1406. pm_runtime_use_autosuspend(&pdev->dev);
  1407. pm_runtime_set_active(&pdev->dev);
  1408. pm_runtime_enable(&pdev->dev);
  1409. /* Register with the SPI framework */
  1410. platform_set_drvdata(pdev, drv_data);
  1411. status = devm_spi_register_master(&pdev->dev, master);
  1412. if (status != 0) {
  1413. dev_err(&pdev->dev, "problem registering spi master\n");
  1414. goto out_error_clock_enabled;
  1415. }
  1416. return status;
  1417. out_error_clock_enabled:
  1418. clk_disable_unprepare(ssp->clk);
  1419. pxa2xx_spi_dma_release(drv_data);
  1420. free_irq(ssp->irq, drv_data);
  1421. out_error_master_alloc:
  1422. spi_master_put(master);
  1423. pxa_ssp_free(ssp);
  1424. return status;
  1425. }
  1426. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1427. {
  1428. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1429. struct ssp_device *ssp;
  1430. if (!drv_data)
  1431. return 0;
  1432. ssp = drv_data->ssp;
  1433. pm_runtime_get_sync(&pdev->dev);
  1434. /* Disable the SSP at the peripheral and SOC level */
  1435. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1436. clk_disable_unprepare(ssp->clk);
  1437. /* Release DMA */
  1438. if (drv_data->master_info->enable_dma)
  1439. pxa2xx_spi_dma_release(drv_data);
  1440. pm_runtime_put_noidle(&pdev->dev);
  1441. pm_runtime_disable(&pdev->dev);
  1442. /* Release IRQ */
  1443. free_irq(ssp->irq, drv_data);
  1444. /* Release SSP */
  1445. pxa_ssp_free(ssp);
  1446. return 0;
  1447. }
  1448. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1449. {
  1450. int status = 0;
  1451. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1452. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1453. }
  1454. #ifdef CONFIG_PM_SLEEP
  1455. static int pxa2xx_spi_suspend(struct device *dev)
  1456. {
  1457. struct driver_data *drv_data = dev_get_drvdata(dev);
  1458. struct ssp_device *ssp = drv_data->ssp;
  1459. int status = 0;
  1460. status = spi_master_suspend(drv_data->master);
  1461. if (status != 0)
  1462. return status;
  1463. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1464. if (!pm_runtime_suspended(dev))
  1465. clk_disable_unprepare(ssp->clk);
  1466. return 0;
  1467. }
  1468. static int pxa2xx_spi_resume(struct device *dev)
  1469. {
  1470. struct driver_data *drv_data = dev_get_drvdata(dev);
  1471. struct ssp_device *ssp = drv_data->ssp;
  1472. int status = 0;
  1473. /* Enable the SSP clock */
  1474. if (!pm_runtime_suspended(dev))
  1475. clk_prepare_enable(ssp->clk);
  1476. /* Restore LPSS private register bits */
  1477. if (is_lpss_ssp(drv_data))
  1478. lpss_ssp_setup(drv_data);
  1479. /* Start the queue running */
  1480. status = spi_master_resume(drv_data->master);
  1481. if (status != 0) {
  1482. dev_err(dev, "problem starting queue (%d)\n", status);
  1483. return status;
  1484. }
  1485. return 0;
  1486. }
  1487. #endif
  1488. #ifdef CONFIG_PM
  1489. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1490. {
  1491. struct driver_data *drv_data = dev_get_drvdata(dev);
  1492. clk_disable_unprepare(drv_data->ssp->clk);
  1493. return 0;
  1494. }
  1495. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1496. {
  1497. struct driver_data *drv_data = dev_get_drvdata(dev);
  1498. clk_prepare_enable(drv_data->ssp->clk);
  1499. return 0;
  1500. }
  1501. #endif
  1502. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1503. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1504. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1505. pxa2xx_spi_runtime_resume, NULL)
  1506. };
  1507. static struct platform_driver driver = {
  1508. .driver = {
  1509. .name = "pxa2xx-spi",
  1510. .pm = &pxa2xx_spi_pm_ops,
  1511. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1512. },
  1513. .probe = pxa2xx_spi_probe,
  1514. .remove = pxa2xx_spi_remove,
  1515. .shutdown = pxa2xx_spi_shutdown,
  1516. };
  1517. static int __init pxa2xx_spi_init(void)
  1518. {
  1519. return platform_driver_register(&driver);
  1520. }
  1521. subsys_initcall(pxa2xx_spi_init);
  1522. static void __exit pxa2xx_spi_exit(void)
  1523. {
  1524. platform_driver_unregister(&driver);
  1525. }
  1526. module_exit(pxa2xx_spi_exit);