spi-pxa2xx.c 46 KB

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