spi.c 52 KB

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