spi.c 44 KB

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