spi.c 51 KB

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