spi.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. * SPI init/core code
  3. *
  4. * Copyright (C) 2005 David Brownell
  5. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/kmod.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/cache.h>
  26. #include <linux/mutex.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/slab.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/export.h>
  35. #include <linux/sched/rt.h>
  36. #include <linux/delay.h>
  37. #include <linux/kthread.h>
  38. #include <linux/ioport.h>
  39. #include <linux/acpi.h>
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/spi.h>
  42. static void spidev_release(struct device *dev)
  43. {
  44. struct spi_device *spi = to_spi_device(dev);
  45. /* spi masters may cleanup for released devices */
  46. if (spi->master->cleanup)
  47. spi->master->cleanup(spi);
  48. spi_master_put(spi->master);
  49. kfree(spi);
  50. }
  51. static ssize_t
  52. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  53. {
  54. const struct spi_device *spi = to_spi_device(dev);
  55. return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
  56. }
  57. static DEVICE_ATTR_RO(modalias);
  58. static struct attribute *spi_dev_attrs[] = {
  59. &dev_attr_modalias.attr,
  60. NULL,
  61. };
  62. ATTRIBUTE_GROUPS(spi_dev);
  63. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
  64. * and the sysfs version makes coldplug work too.
  65. */
  66. static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
  67. const struct spi_device *sdev)
  68. {
  69. while (id->name[0]) {
  70. if (!strcmp(sdev->modalias, id->name))
  71. return id;
  72. id++;
  73. }
  74. return NULL;
  75. }
  76. const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
  77. {
  78. const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
  79. return spi_match_id(sdrv->id_table, sdev);
  80. }
  81. EXPORT_SYMBOL_GPL(spi_get_device_id);
  82. static int spi_match_device(struct device *dev, struct device_driver *drv)
  83. {
  84. const struct spi_device *spi = to_spi_device(dev);
  85. const struct spi_driver *sdrv = to_spi_driver(drv);
  86. /* Attempt an OF style match */
  87. if (of_driver_match_device(dev, drv))
  88. return 1;
  89. /* Then try ACPI */
  90. if (acpi_driver_match_device(dev, drv))
  91. return 1;
  92. if (sdrv->id_table)
  93. return !!spi_match_id(sdrv->id_table, spi);
  94. return strcmp(spi->modalias, drv->name) == 0;
  95. }
  96. static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
  97. {
  98. const struct spi_device *spi = to_spi_device(dev);
  99. add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
  100. return 0;
  101. }
  102. #ifdef CONFIG_PM_SLEEP
  103. static int spi_legacy_suspend(struct device *dev, pm_message_t message)
  104. {
  105. int value = 0;
  106. struct spi_driver *drv = to_spi_driver(dev->driver);
  107. /* suspend will stop irqs and dma; no more i/o */
  108. if (drv) {
  109. if (drv->suspend)
  110. value = drv->suspend(to_spi_device(dev), message);
  111. else
  112. dev_dbg(dev, "... can't suspend\n");
  113. }
  114. return value;
  115. }
  116. static int spi_legacy_resume(struct device *dev)
  117. {
  118. int value = 0;
  119. struct spi_driver *drv = to_spi_driver(dev->driver);
  120. /* resume may restart the i/o queue */
  121. if (drv) {
  122. if (drv->resume)
  123. value = drv->resume(to_spi_device(dev));
  124. else
  125. dev_dbg(dev, "... can't resume\n");
  126. }
  127. return value;
  128. }
  129. static int spi_pm_suspend(struct device *dev)
  130. {
  131. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  132. if (pm)
  133. return pm_generic_suspend(dev);
  134. else
  135. return spi_legacy_suspend(dev, PMSG_SUSPEND);
  136. }
  137. static int spi_pm_resume(struct device *dev)
  138. {
  139. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  140. if (pm)
  141. return pm_generic_resume(dev);
  142. else
  143. return spi_legacy_resume(dev);
  144. }
  145. static int spi_pm_freeze(struct device *dev)
  146. {
  147. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  148. if (pm)
  149. return pm_generic_freeze(dev);
  150. else
  151. return spi_legacy_suspend(dev, PMSG_FREEZE);
  152. }
  153. static int spi_pm_thaw(struct device *dev)
  154. {
  155. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  156. if (pm)
  157. return pm_generic_thaw(dev);
  158. else
  159. return spi_legacy_resume(dev);
  160. }
  161. static int spi_pm_poweroff(struct device *dev)
  162. {
  163. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  164. if (pm)
  165. return pm_generic_poweroff(dev);
  166. else
  167. return spi_legacy_suspend(dev, PMSG_HIBERNATE);
  168. }
  169. static int spi_pm_restore(struct device *dev)
  170. {
  171. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  172. if (pm)
  173. return pm_generic_restore(dev);
  174. else
  175. return spi_legacy_resume(dev);
  176. }
  177. #else
  178. #define spi_pm_suspend NULL
  179. #define spi_pm_resume NULL
  180. #define spi_pm_freeze NULL
  181. #define spi_pm_thaw NULL
  182. #define spi_pm_poweroff NULL
  183. #define spi_pm_restore NULL
  184. #endif
  185. static const struct dev_pm_ops spi_pm = {
  186. .suspend = spi_pm_suspend,
  187. .resume = spi_pm_resume,
  188. .freeze = spi_pm_freeze,
  189. .thaw = spi_pm_thaw,
  190. .poweroff = spi_pm_poweroff,
  191. .restore = spi_pm_restore,
  192. SET_RUNTIME_PM_OPS(
  193. pm_generic_runtime_suspend,
  194. pm_generic_runtime_resume,
  195. NULL
  196. )
  197. };
  198. struct bus_type spi_bus_type = {
  199. .name = "spi",
  200. .dev_groups = spi_dev_groups,
  201. .match = spi_match_device,
  202. .uevent = spi_uevent,
  203. .pm = &spi_pm,
  204. };
  205. EXPORT_SYMBOL_GPL(spi_bus_type);
  206. static int spi_drv_probe(struct device *dev)
  207. {
  208. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  209. struct spi_device *spi = to_spi_device(dev);
  210. int ret;
  211. acpi_dev_pm_attach(&spi->dev, true);
  212. ret = sdrv->probe(spi);
  213. if (ret)
  214. acpi_dev_pm_detach(&spi->dev, true);
  215. return ret;
  216. }
  217. static int spi_drv_remove(struct device *dev)
  218. {
  219. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  220. struct spi_device *spi = to_spi_device(dev);
  221. int ret;
  222. ret = sdrv->remove(spi);
  223. acpi_dev_pm_detach(&spi->dev, true);
  224. return ret;
  225. }
  226. static void spi_drv_shutdown(struct device *dev)
  227. {
  228. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  229. sdrv->shutdown(to_spi_device(dev));
  230. }
  231. /**
  232. * spi_register_driver - register a SPI driver
  233. * @sdrv: the driver to register
  234. * Context: can sleep
  235. */
  236. int spi_register_driver(struct spi_driver *sdrv)
  237. {
  238. sdrv->driver.bus = &spi_bus_type;
  239. if (sdrv->probe)
  240. sdrv->driver.probe = spi_drv_probe;
  241. if (sdrv->remove)
  242. sdrv->driver.remove = spi_drv_remove;
  243. if (sdrv->shutdown)
  244. sdrv->driver.shutdown = spi_drv_shutdown;
  245. return driver_register(&sdrv->driver);
  246. }
  247. EXPORT_SYMBOL_GPL(spi_register_driver);
  248. /*-------------------------------------------------------------------------*/
  249. /* SPI devices should normally not be created by SPI device drivers; that
  250. * would make them board-specific. Similarly with SPI master drivers.
  251. * Device registration normally goes into like arch/.../mach.../board-YYY.c
  252. * with other readonly (flashable) information about mainboard devices.
  253. */
  254. struct boardinfo {
  255. struct list_head list;
  256. struct spi_board_info board_info;
  257. };
  258. static LIST_HEAD(board_list);
  259. static LIST_HEAD(spi_master_list);
  260. /*
  261. * Used to protect add/del opertion for board_info list and
  262. * spi_master list, and their matching process
  263. */
  264. static DEFINE_MUTEX(board_lock);
  265. /**
  266. * spi_alloc_device - Allocate a new SPI device
  267. * @master: Controller to which device is connected
  268. * Context: can sleep
  269. *
  270. * Allows a driver to allocate and initialize a spi_device without
  271. * registering it immediately. This allows a driver to directly
  272. * fill the spi_device with device parameters before calling
  273. * spi_add_device() on it.
  274. *
  275. * Caller is responsible to call spi_add_device() on the returned
  276. * spi_device structure to add it to the SPI master. If the caller
  277. * needs to discard the spi_device without adding it, then it should
  278. * call spi_dev_put() on it.
  279. *
  280. * Returns a pointer to the new device, or NULL.
  281. */
  282. struct spi_device *spi_alloc_device(struct spi_master *master)
  283. {
  284. struct spi_device *spi;
  285. struct device *dev = master->dev.parent;
  286. if (!spi_master_get(master))
  287. return NULL;
  288. spi = kzalloc(sizeof(*spi), GFP_KERNEL);
  289. if (!spi) {
  290. dev_err(dev, "cannot alloc spi_device\n");
  291. spi_master_put(master);
  292. return NULL;
  293. }
  294. spi->master = master;
  295. spi->dev.parent = &master->dev;
  296. spi->dev.bus = &spi_bus_type;
  297. spi->dev.release = spidev_release;
  298. spi->cs_gpio = -ENOENT;
  299. device_initialize(&spi->dev);
  300. return spi;
  301. }
  302. EXPORT_SYMBOL_GPL(spi_alloc_device);
  303. static void spi_dev_set_name(struct spi_device *spi)
  304. {
  305. struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
  306. if (adev) {
  307. dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
  308. return;
  309. }
  310. dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
  311. spi->chip_select);
  312. }
  313. /**
  314. * spi_add_device - Add spi_device allocated with spi_alloc_device
  315. * @spi: spi_device to register
  316. *
  317. * Companion function to spi_alloc_device. Devices allocated with
  318. * spi_alloc_device can be added onto the spi bus with this function.
  319. *
  320. * Returns 0 on success; negative errno on failure
  321. */
  322. int spi_add_device(struct spi_device *spi)
  323. {
  324. static DEFINE_MUTEX(spi_add_lock);
  325. struct spi_master *master = spi->master;
  326. struct device *dev = master->dev.parent;
  327. struct device *d;
  328. int status;
  329. /* Chipselects are numbered 0..max; validate. */
  330. if (spi->chip_select >= master->num_chipselect) {
  331. dev_err(dev, "cs%d >= max %d\n",
  332. spi->chip_select,
  333. master->num_chipselect);
  334. return -EINVAL;
  335. }
  336. /* Set the bus ID string */
  337. spi_dev_set_name(spi);
  338. /* We need to make sure there's no other device with this
  339. * chipselect **BEFORE** we call setup(), else we'll trash
  340. * its configuration. Lock against concurrent add() calls.
  341. */
  342. mutex_lock(&spi_add_lock);
  343. d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
  344. if (d != NULL) {
  345. dev_err(dev, "chipselect %d already in use\n",
  346. spi->chip_select);
  347. put_device(d);
  348. status = -EBUSY;
  349. goto done;
  350. }
  351. if (master->cs_gpios)
  352. spi->cs_gpio = master->cs_gpios[spi->chip_select];
  353. /* Drivers may modify this initial i/o setup, but will
  354. * normally rely on the device being setup. Devices
  355. * using SPI_CS_HIGH can't coexist well otherwise...
  356. */
  357. status = spi_setup(spi);
  358. if (status < 0) {
  359. dev_err(dev, "can't setup %s, status %d\n",
  360. dev_name(&spi->dev), status);
  361. goto done;
  362. }
  363. /* Device may be bound to an active driver when this returns */
  364. status = device_add(&spi->dev);
  365. if (status < 0)
  366. dev_err(dev, "can't add %s, status %d\n",
  367. dev_name(&spi->dev), status);
  368. else
  369. dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
  370. done:
  371. mutex_unlock(&spi_add_lock);
  372. return status;
  373. }
  374. EXPORT_SYMBOL_GPL(spi_add_device);
  375. /**
  376. * spi_new_device - instantiate one new SPI device
  377. * @master: Controller to which device is connected
  378. * @chip: Describes the SPI device
  379. * Context: can sleep
  380. *
  381. * On typical mainboards, this is purely internal; and it's not needed
  382. * after board init creates the hard-wired devices. Some development
  383. * platforms may not be able to use spi_register_board_info though, and
  384. * this is exported so that for example a USB or parport based adapter
  385. * driver could add devices (which it would learn about out-of-band).
  386. *
  387. * Returns the new device, or NULL.
  388. */
  389. struct spi_device *spi_new_device(struct spi_master *master,
  390. struct spi_board_info *chip)
  391. {
  392. struct spi_device *proxy;
  393. int status;
  394. /* NOTE: caller did any chip->bus_num checks necessary.
  395. *
  396. * Also, unless we change the return value convention to use
  397. * error-or-pointer (not NULL-or-pointer), troubleshootability
  398. * suggests syslogged diagnostics are best here (ugh).
  399. */
  400. proxy = spi_alloc_device(master);
  401. if (!proxy)
  402. return NULL;
  403. WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
  404. proxy->chip_select = chip->chip_select;
  405. proxy->max_speed_hz = chip->max_speed_hz;
  406. proxy->mode = chip->mode;
  407. proxy->irq = chip->irq;
  408. strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
  409. proxy->dev.platform_data = (void *) chip->platform_data;
  410. proxy->controller_data = chip->controller_data;
  411. proxy->controller_state = NULL;
  412. status = spi_add_device(proxy);
  413. if (status < 0) {
  414. spi_dev_put(proxy);
  415. return NULL;
  416. }
  417. return proxy;
  418. }
  419. EXPORT_SYMBOL_GPL(spi_new_device);
  420. static void spi_match_master_to_boardinfo(struct spi_master *master,
  421. struct spi_board_info *bi)
  422. {
  423. struct spi_device *dev;
  424. if (master->bus_num != bi->bus_num)
  425. return;
  426. dev = spi_new_device(master, bi);
  427. if (!dev)
  428. dev_err(master->dev.parent, "can't create new device for %s\n",
  429. bi->modalias);
  430. }
  431. /**
  432. * spi_register_board_info - register SPI devices for a given board
  433. * @info: array of chip descriptors
  434. * @n: how many descriptors are provided
  435. * Context: can sleep
  436. *
  437. * Board-specific early init code calls this (probably during arch_initcall)
  438. * with segments of the SPI device table. Any device nodes are created later,
  439. * after the relevant parent SPI controller (bus_num) is defined. We keep
  440. * this table of devices forever, so that reloading a controller driver will
  441. * not make Linux forget about these hard-wired devices.
  442. *
  443. * Other code can also call this, e.g. a particular add-on board might provide
  444. * SPI devices through its expansion connector, so code initializing that board
  445. * would naturally declare its SPI devices.
  446. *
  447. * The board info passed can safely be __initdata ... but be careful of
  448. * any embedded pointers (platform_data, etc), they're copied as-is.
  449. */
  450. int spi_register_board_info(struct spi_board_info const *info, unsigned n)
  451. {
  452. struct boardinfo *bi;
  453. int i;
  454. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  455. if (!bi)
  456. return -ENOMEM;
  457. for (i = 0; i < n; i++, bi++, info++) {
  458. struct spi_master *master;
  459. memcpy(&bi->board_info, info, sizeof(*info));
  460. mutex_lock(&board_lock);
  461. list_add_tail(&bi->list, &board_list);
  462. list_for_each_entry(master, &spi_master_list, list)
  463. spi_match_master_to_boardinfo(master, &bi->board_info);
  464. mutex_unlock(&board_lock);
  465. }
  466. return 0;
  467. }
  468. /*-------------------------------------------------------------------------*/
  469. static void spi_set_cs(struct spi_device *spi, bool enable)
  470. {
  471. if (spi->mode & SPI_CS_HIGH)
  472. enable = !enable;
  473. if (spi->cs_gpio >= 0)
  474. gpio_set_value(spi->cs_gpio, !enable);
  475. else if (spi->master->set_cs)
  476. spi->master->set_cs(spi, !enable);
  477. }
  478. /*
  479. * spi_transfer_one_message - Default implementation of transfer_one_message()
  480. *
  481. * This is a standard implementation of transfer_one_message() for
  482. * drivers which impelment a transfer_one() operation. It provides
  483. * standard handling of delays and chip select management.
  484. */
  485. static int spi_transfer_one_message(struct spi_master *master,
  486. struct spi_message *msg)
  487. {
  488. struct spi_transfer *xfer;
  489. bool cur_cs = true;
  490. bool keep_cs = false;
  491. int ret = 0;
  492. spi_set_cs(msg->spi, true);
  493. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  494. trace_spi_transfer_start(msg, xfer);
  495. reinit_completion(&master->xfer_completion);
  496. ret = master->transfer_one(master, msg->spi, xfer);
  497. if (ret < 0) {
  498. dev_err(&msg->spi->dev,
  499. "SPI transfer failed: %d\n", ret);
  500. goto out;
  501. }
  502. if (ret > 0)
  503. wait_for_completion(&master->xfer_completion);
  504. trace_spi_transfer_stop(msg, xfer);
  505. if (msg->status != -EINPROGRESS)
  506. goto out;
  507. if (xfer->delay_usecs)
  508. udelay(xfer->delay_usecs);
  509. if (xfer->cs_change) {
  510. if (list_is_last(&xfer->transfer_list,
  511. &msg->transfers)) {
  512. keep_cs = true;
  513. } else {
  514. cur_cs = !cur_cs;
  515. spi_set_cs(msg->spi, cur_cs);
  516. }
  517. }
  518. msg->actual_length += xfer->len;
  519. }
  520. out:
  521. if (ret != 0 || !keep_cs)
  522. spi_set_cs(msg->spi, false);
  523. if (msg->status == -EINPROGRESS)
  524. msg->status = ret;
  525. spi_finalize_current_message(master);
  526. return ret;
  527. }
  528. /**
  529. * spi_finalize_current_transfer - report completion of a transfer
  530. *
  531. * Called by SPI drivers using the core transfer_one_message()
  532. * implementation to notify it that the current interrupt driven
  533. * transfer has finised and the next one may be scheduled.
  534. */
  535. void spi_finalize_current_transfer(struct spi_master *master)
  536. {
  537. complete(&master->xfer_completion);
  538. }
  539. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  540. /**
  541. * spi_pump_messages - kthread work function which processes spi message queue
  542. * @work: pointer to kthread work struct contained in the master struct
  543. *
  544. * This function checks if there is any spi message in the queue that
  545. * needs processing and if so call out to the driver to initialize hardware
  546. * and transfer each message.
  547. *
  548. */
  549. static void spi_pump_messages(struct kthread_work *work)
  550. {
  551. struct spi_master *master =
  552. container_of(work, struct spi_master, pump_messages);
  553. unsigned long flags;
  554. bool was_busy = false;
  555. int ret;
  556. /* Lock queue and check for queue work */
  557. spin_lock_irqsave(&master->queue_lock, flags);
  558. if (list_empty(&master->queue) || !master->running) {
  559. if (!master->busy) {
  560. spin_unlock_irqrestore(&master->queue_lock, flags);
  561. return;
  562. }
  563. master->busy = false;
  564. spin_unlock_irqrestore(&master->queue_lock, flags);
  565. if (master->unprepare_transfer_hardware &&
  566. master->unprepare_transfer_hardware(master))
  567. dev_err(&master->dev,
  568. "failed to unprepare transfer hardware\n");
  569. if (master->auto_runtime_pm) {
  570. pm_runtime_mark_last_busy(master->dev.parent);
  571. pm_runtime_put_autosuspend(master->dev.parent);
  572. }
  573. trace_spi_master_idle(master);
  574. return;
  575. }
  576. /* Make sure we are not already running a message */
  577. if (master->cur_msg) {
  578. spin_unlock_irqrestore(&master->queue_lock, flags);
  579. return;
  580. }
  581. /* Extract head of queue */
  582. master->cur_msg =
  583. list_entry(master->queue.next, struct spi_message, queue);
  584. list_del_init(&master->cur_msg->queue);
  585. if (master->busy)
  586. was_busy = true;
  587. else
  588. master->busy = true;
  589. spin_unlock_irqrestore(&master->queue_lock, flags);
  590. if (!was_busy && master->auto_runtime_pm) {
  591. ret = pm_runtime_get_sync(master->dev.parent);
  592. if (ret < 0) {
  593. dev_err(&master->dev, "Failed to power device: %d\n",
  594. ret);
  595. return;
  596. }
  597. }
  598. if (!was_busy)
  599. trace_spi_master_busy(master);
  600. if (!was_busy && master->prepare_transfer_hardware) {
  601. ret = master->prepare_transfer_hardware(master);
  602. if (ret) {
  603. dev_err(&master->dev,
  604. "failed to prepare transfer hardware\n");
  605. if (master->auto_runtime_pm)
  606. pm_runtime_put(master->dev.parent);
  607. return;
  608. }
  609. }
  610. trace_spi_message_start(master->cur_msg);
  611. if (master->prepare_message) {
  612. ret = master->prepare_message(master, master->cur_msg);
  613. if (ret) {
  614. dev_err(&master->dev,
  615. "failed to prepare message: %d\n", ret);
  616. master->cur_msg->status = ret;
  617. spi_finalize_current_message(master);
  618. return;
  619. }
  620. master->cur_msg_prepared = true;
  621. }
  622. ret = master->transfer_one_message(master, master->cur_msg);
  623. if (ret) {
  624. dev_err(&master->dev,
  625. "failed to transfer one message from queue\n");
  626. return;
  627. }
  628. }
  629. static int spi_init_queue(struct spi_master *master)
  630. {
  631. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  632. INIT_LIST_HEAD(&master->queue);
  633. spin_lock_init(&master->queue_lock);
  634. master->running = false;
  635. master->busy = false;
  636. init_kthread_worker(&master->kworker);
  637. master->kworker_task = kthread_run(kthread_worker_fn,
  638. &master->kworker, "%s",
  639. dev_name(&master->dev));
  640. if (IS_ERR(master->kworker_task)) {
  641. dev_err(&master->dev, "failed to create message pump task\n");
  642. return -ENOMEM;
  643. }
  644. init_kthread_work(&master->pump_messages, spi_pump_messages);
  645. /*
  646. * Master config will indicate if this controller should run the
  647. * message pump with high (realtime) priority to reduce the transfer
  648. * latency on the bus by minimising the delay between a transfer
  649. * request and the scheduling of the message pump thread. Without this
  650. * setting the message pump thread will remain at default priority.
  651. */
  652. if (master->rt) {
  653. dev_info(&master->dev,
  654. "will run message pump with realtime priority\n");
  655. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  656. }
  657. return 0;
  658. }
  659. /**
  660. * spi_get_next_queued_message() - called by driver to check for queued
  661. * messages
  662. * @master: the master to check for queued messages
  663. *
  664. * If there are more messages in the queue, the next message is returned from
  665. * this call.
  666. */
  667. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  668. {
  669. struct spi_message *next;
  670. unsigned long flags;
  671. /* get a pointer to the next message, if any */
  672. spin_lock_irqsave(&master->queue_lock, flags);
  673. if (list_empty(&master->queue))
  674. next = NULL;
  675. else
  676. next = list_entry(master->queue.next,
  677. struct spi_message, queue);
  678. spin_unlock_irqrestore(&master->queue_lock, flags);
  679. return next;
  680. }
  681. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  682. /**
  683. * spi_finalize_current_message() - the current message is complete
  684. * @master: the master to return the message to
  685. *
  686. * Called by the driver to notify the core that the message in the front of the
  687. * queue is complete and can be removed from the queue.
  688. */
  689. void spi_finalize_current_message(struct spi_master *master)
  690. {
  691. struct spi_message *mesg;
  692. unsigned long flags;
  693. int ret;
  694. spin_lock_irqsave(&master->queue_lock, flags);
  695. mesg = master->cur_msg;
  696. master->cur_msg = NULL;
  697. queue_kthread_work(&master->kworker, &master->pump_messages);
  698. spin_unlock_irqrestore(&master->queue_lock, flags);
  699. if (master->cur_msg_prepared && master->unprepare_message) {
  700. ret = master->unprepare_message(master, mesg);
  701. if (ret) {
  702. dev_err(&master->dev,
  703. "failed to unprepare message: %d\n", ret);
  704. }
  705. }
  706. master->cur_msg_prepared = false;
  707. mesg->state = NULL;
  708. if (mesg->complete)
  709. mesg->complete(mesg->context);
  710. trace_spi_message_done(mesg);
  711. }
  712. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  713. static int spi_start_queue(struct spi_master *master)
  714. {
  715. unsigned long flags;
  716. spin_lock_irqsave(&master->queue_lock, flags);
  717. if (master->running || master->busy) {
  718. spin_unlock_irqrestore(&master->queue_lock, flags);
  719. return -EBUSY;
  720. }
  721. master->running = true;
  722. master->cur_msg = NULL;
  723. spin_unlock_irqrestore(&master->queue_lock, flags);
  724. queue_kthread_work(&master->kworker, &master->pump_messages);
  725. return 0;
  726. }
  727. static int spi_stop_queue(struct spi_master *master)
  728. {
  729. unsigned long flags;
  730. unsigned limit = 500;
  731. int ret = 0;
  732. spin_lock_irqsave(&master->queue_lock, flags);
  733. /*
  734. * This is a bit lame, but is optimized for the common execution path.
  735. * A wait_queue on the master->busy could be used, but then the common
  736. * execution path (pump_messages) would be required to call wake_up or
  737. * friends on every SPI message. Do this instead.
  738. */
  739. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  740. spin_unlock_irqrestore(&master->queue_lock, flags);
  741. msleep(10);
  742. spin_lock_irqsave(&master->queue_lock, flags);
  743. }
  744. if (!list_empty(&master->queue) || master->busy)
  745. ret = -EBUSY;
  746. else
  747. master->running = false;
  748. spin_unlock_irqrestore(&master->queue_lock, flags);
  749. if (ret) {
  750. dev_warn(&master->dev,
  751. "could not stop message queue\n");
  752. return ret;
  753. }
  754. return ret;
  755. }
  756. static int spi_destroy_queue(struct spi_master *master)
  757. {
  758. int ret;
  759. ret = spi_stop_queue(master);
  760. /*
  761. * flush_kthread_worker will block until all work is done.
  762. * If the reason that stop_queue timed out is that the work will never
  763. * finish, then it does no good to call flush/stop thread, so
  764. * return anyway.
  765. */
  766. if (ret) {
  767. dev_err(&master->dev, "problem destroying queue\n");
  768. return ret;
  769. }
  770. flush_kthread_worker(&master->kworker);
  771. kthread_stop(master->kworker_task);
  772. return 0;
  773. }
  774. /**
  775. * spi_queued_transfer - transfer function for queued transfers
  776. * @spi: spi device which is requesting transfer
  777. * @msg: spi message which is to handled is queued to driver queue
  778. */
  779. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  780. {
  781. struct spi_master *master = spi->master;
  782. unsigned long flags;
  783. spin_lock_irqsave(&master->queue_lock, flags);
  784. if (!master->running) {
  785. spin_unlock_irqrestore(&master->queue_lock, flags);
  786. return -ESHUTDOWN;
  787. }
  788. msg->actual_length = 0;
  789. msg->status = -EINPROGRESS;
  790. list_add_tail(&msg->queue, &master->queue);
  791. if (!master->busy)
  792. queue_kthread_work(&master->kworker, &master->pump_messages);
  793. spin_unlock_irqrestore(&master->queue_lock, flags);
  794. return 0;
  795. }
  796. static int spi_master_initialize_queue(struct spi_master *master)
  797. {
  798. int ret;
  799. master->queued = true;
  800. master->transfer = spi_queued_transfer;
  801. if (!master->transfer_one_message)
  802. master->transfer_one_message = spi_transfer_one_message;
  803. /* Initialize and start queue */
  804. ret = spi_init_queue(master);
  805. if (ret) {
  806. dev_err(&master->dev, "problem initializing queue\n");
  807. goto err_init_queue;
  808. }
  809. ret = spi_start_queue(master);
  810. if (ret) {
  811. dev_err(&master->dev, "problem starting queue\n");
  812. goto err_start_queue;
  813. }
  814. return 0;
  815. err_start_queue:
  816. err_init_queue:
  817. spi_destroy_queue(master);
  818. return ret;
  819. }
  820. /*-------------------------------------------------------------------------*/
  821. #if defined(CONFIG_OF)
  822. /**
  823. * of_register_spi_devices() - Register child devices onto the SPI bus
  824. * @master: Pointer to spi_master device
  825. *
  826. * Registers an spi_device for each child node of master node which has a 'reg'
  827. * property.
  828. */
  829. static void of_register_spi_devices(struct spi_master *master)
  830. {
  831. struct spi_device *spi;
  832. struct device_node *nc;
  833. int rc;
  834. u32 value;
  835. if (!master->dev.of_node)
  836. return;
  837. for_each_available_child_of_node(master->dev.of_node, nc) {
  838. /* Alloc an spi_device */
  839. spi = spi_alloc_device(master);
  840. if (!spi) {
  841. dev_err(&master->dev, "spi_device alloc error for %s\n",
  842. nc->full_name);
  843. spi_dev_put(spi);
  844. continue;
  845. }
  846. /* Select device driver */
  847. if (of_modalias_node(nc, spi->modalias,
  848. sizeof(spi->modalias)) < 0) {
  849. dev_err(&master->dev, "cannot find modalias for %s\n",
  850. nc->full_name);
  851. spi_dev_put(spi);
  852. continue;
  853. }
  854. /* Device address */
  855. rc = of_property_read_u32(nc, "reg", &value);
  856. if (rc) {
  857. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  858. nc->full_name, rc);
  859. spi_dev_put(spi);
  860. continue;
  861. }
  862. spi->chip_select = value;
  863. /* Mode (clock phase/polarity/etc.) */
  864. if (of_find_property(nc, "spi-cpha", NULL))
  865. spi->mode |= SPI_CPHA;
  866. if (of_find_property(nc, "spi-cpol", NULL))
  867. spi->mode |= SPI_CPOL;
  868. if (of_find_property(nc, "spi-cs-high", NULL))
  869. spi->mode |= SPI_CS_HIGH;
  870. if (of_find_property(nc, "spi-3wire", NULL))
  871. spi->mode |= SPI_3WIRE;
  872. /* Device DUAL/QUAD mode */
  873. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  874. switch (value) {
  875. case 1:
  876. break;
  877. case 2:
  878. spi->mode |= SPI_TX_DUAL;
  879. break;
  880. case 4:
  881. spi->mode |= SPI_TX_QUAD;
  882. break;
  883. default:
  884. dev_err(&master->dev,
  885. "spi-tx-bus-width %d not supported\n",
  886. value);
  887. spi_dev_put(spi);
  888. continue;
  889. }
  890. }
  891. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  892. switch (value) {
  893. case 1:
  894. break;
  895. case 2:
  896. spi->mode |= SPI_RX_DUAL;
  897. break;
  898. case 4:
  899. spi->mode |= SPI_RX_QUAD;
  900. break;
  901. default:
  902. dev_err(&master->dev,
  903. "spi-rx-bus-width %d not supported\n",
  904. value);
  905. spi_dev_put(spi);
  906. continue;
  907. }
  908. }
  909. /* Device speed */
  910. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  911. if (rc) {
  912. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  913. nc->full_name, rc);
  914. spi_dev_put(spi);
  915. continue;
  916. }
  917. spi->max_speed_hz = value;
  918. /* IRQ */
  919. spi->irq = irq_of_parse_and_map(nc, 0);
  920. /* Store a pointer to the node in the device structure */
  921. of_node_get(nc);
  922. spi->dev.of_node = nc;
  923. /* Register the new device */
  924. request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
  925. rc = spi_add_device(spi);
  926. if (rc) {
  927. dev_err(&master->dev, "spi_device register error %s\n",
  928. nc->full_name);
  929. spi_dev_put(spi);
  930. }
  931. }
  932. }
  933. #else
  934. static void of_register_spi_devices(struct spi_master *master) { }
  935. #endif
  936. #ifdef CONFIG_ACPI
  937. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  938. {
  939. struct spi_device *spi = data;
  940. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  941. struct acpi_resource_spi_serialbus *sb;
  942. sb = &ares->data.spi_serial_bus;
  943. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  944. spi->chip_select = sb->device_selection;
  945. spi->max_speed_hz = sb->connection_speed;
  946. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  947. spi->mode |= SPI_CPHA;
  948. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  949. spi->mode |= SPI_CPOL;
  950. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  951. spi->mode |= SPI_CS_HIGH;
  952. }
  953. } else if (spi->irq < 0) {
  954. struct resource r;
  955. if (acpi_dev_resource_interrupt(ares, 0, &r))
  956. spi->irq = r.start;
  957. }
  958. /* Always tell the ACPI core to skip this resource */
  959. return 1;
  960. }
  961. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  962. void *data, void **return_value)
  963. {
  964. struct spi_master *master = data;
  965. struct list_head resource_list;
  966. struct acpi_device *adev;
  967. struct spi_device *spi;
  968. int ret;
  969. if (acpi_bus_get_device(handle, &adev))
  970. return AE_OK;
  971. if (acpi_bus_get_status(adev) || !adev->status.present)
  972. return AE_OK;
  973. spi = spi_alloc_device(master);
  974. if (!spi) {
  975. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  976. dev_name(&adev->dev));
  977. return AE_NO_MEMORY;
  978. }
  979. ACPI_COMPANION_SET(&spi->dev, adev);
  980. spi->irq = -1;
  981. INIT_LIST_HEAD(&resource_list);
  982. ret = acpi_dev_get_resources(adev, &resource_list,
  983. acpi_spi_add_resource, spi);
  984. acpi_dev_free_resource_list(&resource_list);
  985. if (ret < 0 || !spi->max_speed_hz) {
  986. spi_dev_put(spi);
  987. return AE_OK;
  988. }
  989. adev->power.flags.ignore_parent = true;
  990. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  991. if (spi_add_device(spi)) {
  992. adev->power.flags.ignore_parent = false;
  993. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  994. dev_name(&adev->dev));
  995. spi_dev_put(spi);
  996. }
  997. return AE_OK;
  998. }
  999. static void acpi_register_spi_devices(struct spi_master *master)
  1000. {
  1001. acpi_status status;
  1002. acpi_handle handle;
  1003. handle = ACPI_HANDLE(master->dev.parent);
  1004. if (!handle)
  1005. return;
  1006. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1007. acpi_spi_add_device, NULL,
  1008. master, NULL);
  1009. if (ACPI_FAILURE(status))
  1010. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1011. }
  1012. #else
  1013. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1014. #endif /* CONFIG_ACPI */
  1015. static void spi_master_release(struct device *dev)
  1016. {
  1017. struct spi_master *master;
  1018. master = container_of(dev, struct spi_master, dev);
  1019. kfree(master);
  1020. }
  1021. static struct class spi_master_class = {
  1022. .name = "spi_master",
  1023. .owner = THIS_MODULE,
  1024. .dev_release = spi_master_release,
  1025. };
  1026. /**
  1027. * spi_alloc_master - allocate SPI master controller
  1028. * @dev: the controller, possibly using the platform_bus
  1029. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1030. * memory is in the driver_data field of the returned device,
  1031. * accessible with spi_master_get_devdata().
  1032. * Context: can sleep
  1033. *
  1034. * This call is used only by SPI master controller drivers, which are the
  1035. * only ones directly touching chip registers. It's how they allocate
  1036. * an spi_master structure, prior to calling spi_register_master().
  1037. *
  1038. * This must be called from context that can sleep. It returns the SPI
  1039. * master structure on success, else NULL.
  1040. *
  1041. * The caller is responsible for assigning the bus number and initializing
  1042. * the master's methods before calling spi_register_master(); and (after errors
  1043. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1044. * leak.
  1045. */
  1046. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1047. {
  1048. struct spi_master *master;
  1049. if (!dev)
  1050. return NULL;
  1051. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1052. if (!master)
  1053. return NULL;
  1054. device_initialize(&master->dev);
  1055. master->bus_num = -1;
  1056. master->num_chipselect = 1;
  1057. master->dev.class = &spi_master_class;
  1058. master->dev.parent = get_device(dev);
  1059. spi_master_set_devdata(master, &master[1]);
  1060. return master;
  1061. }
  1062. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1063. #ifdef CONFIG_OF
  1064. static int of_spi_register_master(struct spi_master *master)
  1065. {
  1066. int nb, i, *cs;
  1067. struct device_node *np = master->dev.of_node;
  1068. if (!np)
  1069. return 0;
  1070. nb = of_gpio_named_count(np, "cs-gpios");
  1071. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1072. /* Return error only for an incorrectly formed cs-gpios property */
  1073. if (nb == 0 || nb == -ENOENT)
  1074. return 0;
  1075. else if (nb < 0)
  1076. return nb;
  1077. cs = devm_kzalloc(&master->dev,
  1078. sizeof(int) * master->num_chipselect,
  1079. GFP_KERNEL);
  1080. master->cs_gpios = cs;
  1081. if (!master->cs_gpios)
  1082. return -ENOMEM;
  1083. for (i = 0; i < master->num_chipselect; i++)
  1084. cs[i] = -ENOENT;
  1085. for (i = 0; i < nb; i++)
  1086. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1087. return 0;
  1088. }
  1089. #else
  1090. static int of_spi_register_master(struct spi_master *master)
  1091. {
  1092. return 0;
  1093. }
  1094. #endif
  1095. /**
  1096. * spi_register_master - register SPI master controller
  1097. * @master: initialized master, originally from spi_alloc_master()
  1098. * Context: can sleep
  1099. *
  1100. * SPI master controllers connect to their drivers using some non-SPI bus,
  1101. * such as the platform bus. The final stage of probe() in that code
  1102. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1103. *
  1104. * SPI controllers use board specific (often SOC specific) bus numbers,
  1105. * and board-specific addressing for SPI devices combines those numbers
  1106. * with chip select numbers. Since SPI does not directly support dynamic
  1107. * device identification, boards need configuration tables telling which
  1108. * chip is at which address.
  1109. *
  1110. * This must be called from context that can sleep. It returns zero on
  1111. * success, else a negative error code (dropping the master's refcount).
  1112. * After a successful return, the caller is responsible for calling
  1113. * spi_unregister_master().
  1114. */
  1115. int spi_register_master(struct spi_master *master)
  1116. {
  1117. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1118. struct device *dev = master->dev.parent;
  1119. struct boardinfo *bi;
  1120. int status = -ENODEV;
  1121. int dynamic = 0;
  1122. if (!dev)
  1123. return -ENODEV;
  1124. status = of_spi_register_master(master);
  1125. if (status)
  1126. return status;
  1127. /* even if it's just one always-selected device, there must
  1128. * be at least one chipselect
  1129. */
  1130. if (master->num_chipselect == 0)
  1131. return -EINVAL;
  1132. if ((master->bus_num < 0) && master->dev.of_node)
  1133. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1134. /* convention: dynamically assigned bus IDs count down from the max */
  1135. if (master->bus_num < 0) {
  1136. /* FIXME switch to an IDR based scheme, something like
  1137. * I2C now uses, so we can't run out of "dynamic" IDs
  1138. */
  1139. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1140. dynamic = 1;
  1141. }
  1142. spin_lock_init(&master->bus_lock_spinlock);
  1143. mutex_init(&master->bus_lock_mutex);
  1144. master->bus_lock_flag = 0;
  1145. init_completion(&master->xfer_completion);
  1146. /* register the device, then userspace will see it.
  1147. * registration fails if the bus ID is in use.
  1148. */
  1149. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1150. status = device_add(&master->dev);
  1151. if (status < 0)
  1152. goto done;
  1153. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1154. dynamic ? " (dynamic)" : "");
  1155. /* If we're using a queued driver, start the queue */
  1156. if (master->transfer)
  1157. dev_info(dev, "master is unqueued, this is deprecated\n");
  1158. else {
  1159. status = spi_master_initialize_queue(master);
  1160. if (status) {
  1161. device_del(&master->dev);
  1162. goto done;
  1163. }
  1164. }
  1165. mutex_lock(&board_lock);
  1166. list_add_tail(&master->list, &spi_master_list);
  1167. list_for_each_entry(bi, &board_list, list)
  1168. spi_match_master_to_boardinfo(master, &bi->board_info);
  1169. mutex_unlock(&board_lock);
  1170. /* Register devices from the device tree and ACPI */
  1171. of_register_spi_devices(master);
  1172. acpi_register_spi_devices(master);
  1173. done:
  1174. return status;
  1175. }
  1176. EXPORT_SYMBOL_GPL(spi_register_master);
  1177. static void devm_spi_unregister(struct device *dev, void *res)
  1178. {
  1179. spi_unregister_master(*(struct spi_master **)res);
  1180. }
  1181. /**
  1182. * dev_spi_register_master - register managed SPI master controller
  1183. * @dev: device managing SPI master
  1184. * @master: initialized master, originally from spi_alloc_master()
  1185. * Context: can sleep
  1186. *
  1187. * Register a SPI device as with spi_register_master() which will
  1188. * automatically be unregister
  1189. */
  1190. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1191. {
  1192. struct spi_master **ptr;
  1193. int ret;
  1194. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1195. if (!ptr)
  1196. return -ENOMEM;
  1197. ret = spi_register_master(master);
  1198. if (!ret) {
  1199. *ptr = master;
  1200. devres_add(dev, ptr);
  1201. } else {
  1202. devres_free(ptr);
  1203. }
  1204. return ret;
  1205. }
  1206. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1207. static int __unregister(struct device *dev, void *null)
  1208. {
  1209. spi_unregister_device(to_spi_device(dev));
  1210. return 0;
  1211. }
  1212. /**
  1213. * spi_unregister_master - unregister SPI master controller
  1214. * @master: the master being unregistered
  1215. * Context: can sleep
  1216. *
  1217. * This call is used only by SPI master controller drivers, which are the
  1218. * only ones directly touching chip registers.
  1219. *
  1220. * This must be called from context that can sleep.
  1221. */
  1222. void spi_unregister_master(struct spi_master *master)
  1223. {
  1224. int dummy;
  1225. if (master->queued) {
  1226. if (spi_destroy_queue(master))
  1227. dev_err(&master->dev, "queue remove failed\n");
  1228. }
  1229. mutex_lock(&board_lock);
  1230. list_del(&master->list);
  1231. mutex_unlock(&board_lock);
  1232. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1233. device_unregister(&master->dev);
  1234. }
  1235. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1236. int spi_master_suspend(struct spi_master *master)
  1237. {
  1238. int ret;
  1239. /* Basically no-ops for non-queued masters */
  1240. if (!master->queued)
  1241. return 0;
  1242. ret = spi_stop_queue(master);
  1243. if (ret)
  1244. dev_err(&master->dev, "queue stop failed\n");
  1245. return ret;
  1246. }
  1247. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1248. int spi_master_resume(struct spi_master *master)
  1249. {
  1250. int ret;
  1251. if (!master->queued)
  1252. return 0;
  1253. ret = spi_start_queue(master);
  1254. if (ret)
  1255. dev_err(&master->dev, "queue restart failed\n");
  1256. return ret;
  1257. }
  1258. EXPORT_SYMBOL_GPL(spi_master_resume);
  1259. static int __spi_master_match(struct device *dev, const void *data)
  1260. {
  1261. struct spi_master *m;
  1262. const u16 *bus_num = data;
  1263. m = container_of(dev, struct spi_master, dev);
  1264. return m->bus_num == *bus_num;
  1265. }
  1266. /**
  1267. * spi_busnum_to_master - look up master associated with bus_num
  1268. * @bus_num: the master's bus number
  1269. * Context: can sleep
  1270. *
  1271. * This call may be used with devices that are registered after
  1272. * arch init time. It returns a refcounted pointer to the relevant
  1273. * spi_master (which the caller must release), or NULL if there is
  1274. * no such master registered.
  1275. */
  1276. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1277. {
  1278. struct device *dev;
  1279. struct spi_master *master = NULL;
  1280. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1281. __spi_master_match);
  1282. if (dev)
  1283. master = container_of(dev, struct spi_master, dev);
  1284. /* reference got in class_find_device */
  1285. return master;
  1286. }
  1287. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1288. /*-------------------------------------------------------------------------*/
  1289. /* Core methods for SPI master protocol drivers. Some of the
  1290. * other core methods are currently defined as inline functions.
  1291. */
  1292. /**
  1293. * spi_setup - setup SPI mode and clock rate
  1294. * @spi: the device whose settings are being modified
  1295. * Context: can sleep, and no requests are queued to the device
  1296. *
  1297. * SPI protocol drivers may need to update the transfer mode if the
  1298. * device doesn't work with its default. They may likewise need
  1299. * to update clock rates or word sizes from initial values. This function
  1300. * changes those settings, and must be called from a context that can sleep.
  1301. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1302. * effect the next time the device is selected and data is transferred to
  1303. * or from it. When this function returns, the spi device is deselected.
  1304. *
  1305. * Note that this call will fail if the protocol driver specifies an option
  1306. * that the underlying controller or its driver does not support. For
  1307. * example, not all hardware supports wire transfers using nine bit words,
  1308. * LSB-first wire encoding, or active-high chipselects.
  1309. */
  1310. int spi_setup(struct spi_device *spi)
  1311. {
  1312. unsigned bad_bits;
  1313. int status = 0;
  1314. /* check mode to prevent that DUAL and QUAD set at the same time
  1315. */
  1316. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1317. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1318. dev_err(&spi->dev,
  1319. "setup: can not select dual and quad at the same time\n");
  1320. return -EINVAL;
  1321. }
  1322. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1323. */
  1324. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1325. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1326. return -EINVAL;
  1327. /* help drivers fail *cleanly* when they need options
  1328. * that aren't supported with their current master
  1329. */
  1330. bad_bits = spi->mode & ~spi->master->mode_bits;
  1331. if (bad_bits) {
  1332. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1333. bad_bits);
  1334. return -EINVAL;
  1335. }
  1336. if (!spi->bits_per_word)
  1337. spi->bits_per_word = 8;
  1338. if (spi->master->setup)
  1339. status = spi->master->setup(spi);
  1340. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1341. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1342. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1343. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1344. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1345. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1346. spi->bits_per_word, spi->max_speed_hz,
  1347. status);
  1348. return status;
  1349. }
  1350. EXPORT_SYMBOL_GPL(spi_setup);
  1351. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1352. {
  1353. struct spi_master *master = spi->master;
  1354. struct spi_transfer *xfer;
  1355. message->spi = spi;
  1356. trace_spi_message_submit(message);
  1357. if (list_empty(&message->transfers))
  1358. return -EINVAL;
  1359. if (!message->complete)
  1360. return -EINVAL;
  1361. /* Half-duplex links include original MicroWire, and ones with
  1362. * only one data pin like SPI_3WIRE (switches direction) or where
  1363. * either MOSI or MISO is missing. They can also be caused by
  1364. * software limitations.
  1365. */
  1366. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1367. || (spi->mode & SPI_3WIRE)) {
  1368. unsigned flags = master->flags;
  1369. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1370. if (xfer->rx_buf && xfer->tx_buf)
  1371. return -EINVAL;
  1372. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1373. return -EINVAL;
  1374. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1375. return -EINVAL;
  1376. }
  1377. }
  1378. /**
  1379. * Set transfer bits_per_word and max speed as spi device default if
  1380. * it is not set for this transfer.
  1381. * Set transfer tx_nbits and rx_nbits as single transfer default
  1382. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1383. */
  1384. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1385. message->frame_length += xfer->len;
  1386. if (!xfer->bits_per_word)
  1387. xfer->bits_per_word = spi->bits_per_word;
  1388. if (!xfer->speed_hz) {
  1389. xfer->speed_hz = spi->max_speed_hz;
  1390. if (master->max_speed_hz &&
  1391. xfer->speed_hz > master->max_speed_hz)
  1392. xfer->speed_hz = master->max_speed_hz;
  1393. }
  1394. if (master->bits_per_word_mask) {
  1395. /* Only 32 bits fit in the mask */
  1396. if (xfer->bits_per_word > 32)
  1397. return -EINVAL;
  1398. if (!(master->bits_per_word_mask &
  1399. BIT(xfer->bits_per_word - 1)))
  1400. return -EINVAL;
  1401. }
  1402. if (xfer->speed_hz && master->min_speed_hz &&
  1403. xfer->speed_hz < master->min_speed_hz)
  1404. return -EINVAL;
  1405. if (xfer->speed_hz && master->max_speed_hz &&
  1406. xfer->speed_hz > master->max_speed_hz)
  1407. return -EINVAL;
  1408. if (xfer->tx_buf && !xfer->tx_nbits)
  1409. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1410. if (xfer->rx_buf && !xfer->rx_nbits)
  1411. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1412. /* check transfer tx/rx_nbits:
  1413. * 1. keep the value is not out of single, dual and quad
  1414. * 2. keep tx/rx_nbits is contained by mode in spi_device
  1415. * 3. if SPI_3WIRE, tx/rx_nbits should be in single
  1416. */
  1417. if (xfer->tx_buf) {
  1418. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1419. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1420. xfer->tx_nbits != SPI_NBITS_QUAD)
  1421. return -EINVAL;
  1422. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1423. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1424. return -EINVAL;
  1425. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1426. !(spi->mode & SPI_TX_QUAD))
  1427. return -EINVAL;
  1428. if ((spi->mode & SPI_3WIRE) &&
  1429. (xfer->tx_nbits != SPI_NBITS_SINGLE))
  1430. return -EINVAL;
  1431. }
  1432. /* check transfer rx_nbits */
  1433. if (xfer->rx_buf) {
  1434. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1435. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1436. xfer->rx_nbits != SPI_NBITS_QUAD)
  1437. return -EINVAL;
  1438. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1439. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1440. return -EINVAL;
  1441. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1442. !(spi->mode & SPI_RX_QUAD))
  1443. return -EINVAL;
  1444. if ((spi->mode & SPI_3WIRE) &&
  1445. (xfer->rx_nbits != SPI_NBITS_SINGLE))
  1446. return -EINVAL;
  1447. }
  1448. }
  1449. message->status = -EINPROGRESS;
  1450. return master->transfer(spi, message);
  1451. }
  1452. /**
  1453. * spi_async - asynchronous SPI transfer
  1454. * @spi: device with which data will be exchanged
  1455. * @message: describes the data transfers, including completion callback
  1456. * Context: any (irqs may be blocked, etc)
  1457. *
  1458. * This call may be used in_irq and other contexts which can't sleep,
  1459. * as well as from task contexts which can sleep.
  1460. *
  1461. * The completion callback is invoked in a context which can't sleep.
  1462. * Before that invocation, the value of message->status is undefined.
  1463. * When the callback is issued, message->status holds either zero (to
  1464. * indicate complete success) or a negative error code. After that
  1465. * callback returns, the driver which issued the transfer request may
  1466. * deallocate the associated memory; it's no longer in use by any SPI
  1467. * core or controller driver code.
  1468. *
  1469. * Note that although all messages to a spi_device are handled in
  1470. * FIFO order, messages may go to different devices in other orders.
  1471. * Some device might be higher priority, or have various "hard" access
  1472. * time requirements, for example.
  1473. *
  1474. * On detection of any fault during the transfer, processing of
  1475. * the entire message is aborted, and the device is deselected.
  1476. * Until returning from the associated message completion callback,
  1477. * no other spi_message queued to that device will be processed.
  1478. * (This rule applies equally to all the synchronous transfer calls,
  1479. * which are wrappers around this core asynchronous primitive.)
  1480. */
  1481. int spi_async(struct spi_device *spi, struct spi_message *message)
  1482. {
  1483. struct spi_master *master = spi->master;
  1484. int ret;
  1485. unsigned long flags;
  1486. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1487. if (master->bus_lock_flag)
  1488. ret = -EBUSY;
  1489. else
  1490. ret = __spi_async(spi, message);
  1491. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1492. return ret;
  1493. }
  1494. EXPORT_SYMBOL_GPL(spi_async);
  1495. /**
  1496. * spi_async_locked - version of spi_async with exclusive bus usage
  1497. * @spi: device with which data will be exchanged
  1498. * @message: describes the data transfers, including completion callback
  1499. * Context: any (irqs may be blocked, etc)
  1500. *
  1501. * This call may be used in_irq and other contexts which can't sleep,
  1502. * as well as from task contexts which can sleep.
  1503. *
  1504. * The completion callback is invoked in a context which can't sleep.
  1505. * Before that invocation, the value of message->status is undefined.
  1506. * When the callback is issued, message->status holds either zero (to
  1507. * indicate complete success) or a negative error code. After that
  1508. * callback returns, the driver which issued the transfer request may
  1509. * deallocate the associated memory; it's no longer in use by any SPI
  1510. * core or controller driver code.
  1511. *
  1512. * Note that although all messages to a spi_device are handled in
  1513. * FIFO order, messages may go to different devices in other orders.
  1514. * Some device might be higher priority, or have various "hard" access
  1515. * time requirements, for example.
  1516. *
  1517. * On detection of any fault during the transfer, processing of
  1518. * the entire message is aborted, and the device is deselected.
  1519. * Until returning from the associated message completion callback,
  1520. * no other spi_message queued to that device will be processed.
  1521. * (This rule applies equally to all the synchronous transfer calls,
  1522. * which are wrappers around this core asynchronous primitive.)
  1523. */
  1524. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1525. {
  1526. struct spi_master *master = spi->master;
  1527. int ret;
  1528. unsigned long flags;
  1529. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1530. ret = __spi_async(spi, message);
  1531. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1532. return ret;
  1533. }
  1534. EXPORT_SYMBOL_GPL(spi_async_locked);
  1535. /*-------------------------------------------------------------------------*/
  1536. /* Utility methods for SPI master protocol drivers, layered on
  1537. * top of the core. Some other utility methods are defined as
  1538. * inline functions.
  1539. */
  1540. static void spi_complete(void *arg)
  1541. {
  1542. complete(arg);
  1543. }
  1544. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1545. int bus_locked)
  1546. {
  1547. DECLARE_COMPLETION_ONSTACK(done);
  1548. int status;
  1549. struct spi_master *master = spi->master;
  1550. message->complete = spi_complete;
  1551. message->context = &done;
  1552. if (!bus_locked)
  1553. mutex_lock(&master->bus_lock_mutex);
  1554. status = spi_async_locked(spi, message);
  1555. if (!bus_locked)
  1556. mutex_unlock(&master->bus_lock_mutex);
  1557. if (status == 0) {
  1558. wait_for_completion(&done);
  1559. status = message->status;
  1560. }
  1561. message->context = NULL;
  1562. return status;
  1563. }
  1564. /**
  1565. * spi_sync - blocking/synchronous SPI data transfers
  1566. * @spi: device with which data will be exchanged
  1567. * @message: describes the data transfers
  1568. * Context: can sleep
  1569. *
  1570. * This call may only be used from a context that may sleep. The sleep
  1571. * is non-interruptible, and has no timeout. Low-overhead controller
  1572. * drivers may DMA directly into and out of the message buffers.
  1573. *
  1574. * Note that the SPI device's chip select is active during the message,
  1575. * and then is normally disabled between messages. Drivers for some
  1576. * frequently-used devices may want to minimize costs of selecting a chip,
  1577. * by leaving it selected in anticipation that the next message will go
  1578. * to the same chip. (That may increase power usage.)
  1579. *
  1580. * Also, the caller is guaranteeing that the memory associated with the
  1581. * message will not be freed before this call returns.
  1582. *
  1583. * It returns zero on success, else a negative error code.
  1584. */
  1585. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1586. {
  1587. return __spi_sync(spi, message, 0);
  1588. }
  1589. EXPORT_SYMBOL_GPL(spi_sync);
  1590. /**
  1591. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1592. * @spi: device with which data will be exchanged
  1593. * @message: describes the data transfers
  1594. * Context: can sleep
  1595. *
  1596. * This call may only be used from a context that may sleep. The sleep
  1597. * is non-interruptible, and has no timeout. Low-overhead controller
  1598. * drivers may DMA directly into and out of the message buffers.
  1599. *
  1600. * This call should be used by drivers that require exclusive access to the
  1601. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1602. * be released by a spi_bus_unlock call when the exclusive access is over.
  1603. *
  1604. * It returns zero on success, else a negative error code.
  1605. */
  1606. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1607. {
  1608. return __spi_sync(spi, message, 1);
  1609. }
  1610. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1611. /**
  1612. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1613. * @master: SPI bus master that should be locked for exclusive bus access
  1614. * Context: can sleep
  1615. *
  1616. * This call may only be used from a context that may sleep. The sleep
  1617. * is non-interruptible, and has no timeout.
  1618. *
  1619. * This call should be used by drivers that require exclusive access to the
  1620. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1621. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1622. * and spi_async_locked calls when the SPI bus lock is held.
  1623. *
  1624. * It returns zero on success, else a negative error code.
  1625. */
  1626. int spi_bus_lock(struct spi_master *master)
  1627. {
  1628. unsigned long flags;
  1629. mutex_lock(&master->bus_lock_mutex);
  1630. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1631. master->bus_lock_flag = 1;
  1632. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1633. /* mutex remains locked until spi_bus_unlock is called */
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1637. /**
  1638. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1639. * @master: SPI bus master that was locked for exclusive bus access
  1640. * Context: can sleep
  1641. *
  1642. * This call may only be used from a context that may sleep. The sleep
  1643. * is non-interruptible, and has no timeout.
  1644. *
  1645. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1646. * call.
  1647. *
  1648. * It returns zero on success, else a negative error code.
  1649. */
  1650. int spi_bus_unlock(struct spi_master *master)
  1651. {
  1652. master->bus_lock_flag = 0;
  1653. mutex_unlock(&master->bus_lock_mutex);
  1654. return 0;
  1655. }
  1656. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1657. /* portable code must never pass more than 32 bytes */
  1658. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1659. static u8 *buf;
  1660. /**
  1661. * spi_write_then_read - SPI synchronous write followed by read
  1662. * @spi: device with which data will be exchanged
  1663. * @txbuf: data to be written (need not be dma-safe)
  1664. * @n_tx: size of txbuf, in bytes
  1665. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1666. * @n_rx: size of rxbuf, in bytes
  1667. * Context: can sleep
  1668. *
  1669. * This performs a half duplex MicroWire style transaction with the
  1670. * device, sending txbuf and then reading rxbuf. The return value
  1671. * is zero for success, else a negative errno status code.
  1672. * This call may only be used from a context that may sleep.
  1673. *
  1674. * Parameters to this routine are always copied using a small buffer;
  1675. * portable code should never use this for more than 32 bytes.
  1676. * Performance-sensitive or bulk transfer code should instead use
  1677. * spi_{async,sync}() calls with dma-safe buffers.
  1678. */
  1679. int spi_write_then_read(struct spi_device *spi,
  1680. const void *txbuf, unsigned n_tx,
  1681. void *rxbuf, unsigned n_rx)
  1682. {
  1683. static DEFINE_MUTEX(lock);
  1684. int status;
  1685. struct spi_message message;
  1686. struct spi_transfer x[2];
  1687. u8 *local_buf;
  1688. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1689. * copying here, (as a pure convenience thing), but we can
  1690. * keep heap costs out of the hot path unless someone else is
  1691. * using the pre-allocated buffer or the transfer is too large.
  1692. */
  1693. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1694. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1695. GFP_KERNEL | GFP_DMA);
  1696. if (!local_buf)
  1697. return -ENOMEM;
  1698. } else {
  1699. local_buf = buf;
  1700. }
  1701. spi_message_init(&message);
  1702. memset(x, 0, sizeof(x));
  1703. if (n_tx) {
  1704. x[0].len = n_tx;
  1705. spi_message_add_tail(&x[0], &message);
  1706. }
  1707. if (n_rx) {
  1708. x[1].len = n_rx;
  1709. spi_message_add_tail(&x[1], &message);
  1710. }
  1711. memcpy(local_buf, txbuf, n_tx);
  1712. x[0].tx_buf = local_buf;
  1713. x[1].rx_buf = local_buf + n_tx;
  1714. /* do the i/o */
  1715. status = spi_sync(spi, &message);
  1716. if (status == 0)
  1717. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1718. if (x[0].tx_buf == buf)
  1719. mutex_unlock(&lock);
  1720. else
  1721. kfree(local_buf);
  1722. return status;
  1723. }
  1724. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1725. /*-------------------------------------------------------------------------*/
  1726. static int __init spi_init(void)
  1727. {
  1728. int status;
  1729. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1730. if (!buf) {
  1731. status = -ENOMEM;
  1732. goto err0;
  1733. }
  1734. status = bus_register(&spi_bus_type);
  1735. if (status < 0)
  1736. goto err1;
  1737. status = class_register(&spi_master_class);
  1738. if (status < 0)
  1739. goto err2;
  1740. return 0;
  1741. err2:
  1742. bus_unregister(&spi_bus_type);
  1743. err1:
  1744. kfree(buf);
  1745. buf = NULL;
  1746. err0:
  1747. return status;
  1748. }
  1749. /* board_info is normally registered in arch_initcall(),
  1750. * but even essential drivers wait till later
  1751. *
  1752. * REVISIT only boardinfo really needs static linking. the rest (device and
  1753. * driver registration) _could_ be dynamically linked (modular) ... costs
  1754. * include needing to have boardinfo data structures be much more public.
  1755. */
  1756. postcore_initcall(spi_init);