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. if (of_find_property(nc, "spi-3wire", NULL))
  734. spi->mode |= SPI_3WIRE;
  735. /* Device speed */
  736. prop = of_get_property(nc, "spi-max-frequency", &len);
  737. if (!prop || len < sizeof(*prop)) {
  738. dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
  739. nc->full_name);
  740. spi_dev_put(spi);
  741. continue;
  742. }
  743. spi->max_speed_hz = be32_to_cpup(prop);
  744. /* IRQ */
  745. spi->irq = irq_of_parse_and_map(nc, 0);
  746. /* Store a pointer to the node in the device structure */
  747. of_node_get(nc);
  748. spi->dev.of_node = nc;
  749. /* Register the new device */
  750. request_module(spi->modalias);
  751. rc = spi_add_device(spi);
  752. if (rc) {
  753. dev_err(&master->dev, "spi_device register error %s\n",
  754. nc->full_name);
  755. spi_dev_put(spi);
  756. }
  757. }
  758. }
  759. #else
  760. static void of_register_spi_devices(struct spi_master *master) { }
  761. #endif
  762. static void spi_master_release(struct device *dev)
  763. {
  764. struct spi_master *master;
  765. master = container_of(dev, struct spi_master, dev);
  766. kfree(master);
  767. }
  768. static struct class spi_master_class = {
  769. .name = "spi_master",
  770. .owner = THIS_MODULE,
  771. .dev_release = spi_master_release,
  772. };
  773. /**
  774. * spi_alloc_master - allocate SPI master controller
  775. * @dev: the controller, possibly using the platform_bus
  776. * @size: how much zeroed driver-private data to allocate; the pointer to this
  777. * memory is in the driver_data field of the returned device,
  778. * accessible with spi_master_get_devdata().
  779. * Context: can sleep
  780. *
  781. * This call is used only by SPI master controller drivers, which are the
  782. * only ones directly touching chip registers. It's how they allocate
  783. * an spi_master structure, prior to calling spi_register_master().
  784. *
  785. * This must be called from context that can sleep. It returns the SPI
  786. * master structure on success, else NULL.
  787. *
  788. * The caller is responsible for assigning the bus number and initializing
  789. * the master's methods before calling spi_register_master(); and (after errors
  790. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  791. * leak.
  792. */
  793. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  794. {
  795. struct spi_master *master;
  796. if (!dev)
  797. return NULL;
  798. master = kzalloc(size + sizeof *master, GFP_KERNEL);
  799. if (!master)
  800. return NULL;
  801. device_initialize(&master->dev);
  802. master->bus_num = -1;
  803. master->num_chipselect = 1;
  804. master->dev.class = &spi_master_class;
  805. master->dev.parent = get_device(dev);
  806. spi_master_set_devdata(master, &master[1]);
  807. return master;
  808. }
  809. EXPORT_SYMBOL_GPL(spi_alloc_master);
  810. #ifdef CONFIG_OF
  811. static int of_spi_register_master(struct spi_master *master)
  812. {
  813. u16 nb;
  814. int i, *cs;
  815. struct device_node *np = master->dev.of_node;
  816. if (!np)
  817. return 0;
  818. nb = of_gpio_named_count(np, "cs-gpios");
  819. master->num_chipselect = max(nb, master->num_chipselect);
  820. if (nb < 1)
  821. return 0;
  822. cs = devm_kzalloc(&master->dev,
  823. sizeof(int) * master->num_chipselect,
  824. GFP_KERNEL);
  825. master->cs_gpios = cs;
  826. if (!master->cs_gpios)
  827. return -ENOMEM;
  828. memset(cs, -EINVAL, master->num_chipselect);
  829. for (i = 0; i < nb; i++)
  830. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  831. return 0;
  832. }
  833. #else
  834. static int of_spi_register_master(struct spi_master *master)
  835. {
  836. return 0;
  837. }
  838. #endif
  839. /**
  840. * spi_register_master - register SPI master controller
  841. * @master: initialized master, originally from spi_alloc_master()
  842. * Context: can sleep
  843. *
  844. * SPI master controllers connect to their drivers using some non-SPI bus,
  845. * such as the platform bus. The final stage of probe() in that code
  846. * includes calling spi_register_master() to hook up to this SPI bus glue.
  847. *
  848. * SPI controllers use board specific (often SOC specific) bus numbers,
  849. * and board-specific addressing for SPI devices combines those numbers
  850. * with chip select numbers. Since SPI does not directly support dynamic
  851. * device identification, boards need configuration tables telling which
  852. * chip is at which address.
  853. *
  854. * This must be called from context that can sleep. It returns zero on
  855. * success, else a negative error code (dropping the master's refcount).
  856. * After a successful return, the caller is responsible for calling
  857. * spi_unregister_master().
  858. */
  859. int spi_register_master(struct spi_master *master)
  860. {
  861. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  862. struct device *dev = master->dev.parent;
  863. struct boardinfo *bi;
  864. int status = -ENODEV;
  865. int dynamic = 0;
  866. if (!dev)
  867. return -ENODEV;
  868. status = of_spi_register_master(master);
  869. if (status)
  870. return status;
  871. /* even if it's just one always-selected device, there must
  872. * be at least one chipselect
  873. */
  874. if (master->num_chipselect == 0)
  875. return -EINVAL;
  876. /* convention: dynamically assigned bus IDs count down from the max */
  877. if (master->bus_num < 0) {
  878. /* FIXME switch to an IDR based scheme, something like
  879. * I2C now uses, so we can't run out of "dynamic" IDs
  880. */
  881. master->bus_num = atomic_dec_return(&dyn_bus_id);
  882. dynamic = 1;
  883. }
  884. spin_lock_init(&master->bus_lock_spinlock);
  885. mutex_init(&master->bus_lock_mutex);
  886. master->bus_lock_flag = 0;
  887. /* register the device, then userspace will see it.
  888. * registration fails if the bus ID is in use.
  889. */
  890. dev_set_name(&master->dev, "spi%u", master->bus_num);
  891. status = device_add(&master->dev);
  892. if (status < 0)
  893. goto done;
  894. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  895. dynamic ? " (dynamic)" : "");
  896. /* If we're using a queued driver, start the queue */
  897. if (master->transfer)
  898. dev_info(dev, "master is unqueued, this is deprecated\n");
  899. else {
  900. status = spi_master_initialize_queue(master);
  901. if (status) {
  902. device_unregister(&master->dev);
  903. goto done;
  904. }
  905. }
  906. mutex_lock(&board_lock);
  907. list_add_tail(&master->list, &spi_master_list);
  908. list_for_each_entry(bi, &board_list, list)
  909. spi_match_master_to_boardinfo(master, &bi->board_info);
  910. mutex_unlock(&board_lock);
  911. /* Register devices from the device tree */
  912. of_register_spi_devices(master);
  913. done:
  914. return status;
  915. }
  916. EXPORT_SYMBOL_GPL(spi_register_master);
  917. static int __unregister(struct device *dev, void *null)
  918. {
  919. spi_unregister_device(to_spi_device(dev));
  920. return 0;
  921. }
  922. /**
  923. * spi_unregister_master - unregister SPI master controller
  924. * @master: the master being unregistered
  925. * Context: can sleep
  926. *
  927. * This call is used only by SPI master controller drivers, which are the
  928. * only ones directly touching chip registers.
  929. *
  930. * This must be called from context that can sleep.
  931. */
  932. void spi_unregister_master(struct spi_master *master)
  933. {
  934. int dummy;
  935. if (master->queued) {
  936. if (spi_destroy_queue(master))
  937. dev_err(&master->dev, "queue remove failed\n");
  938. }
  939. mutex_lock(&board_lock);
  940. list_del(&master->list);
  941. mutex_unlock(&board_lock);
  942. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  943. device_unregister(&master->dev);
  944. }
  945. EXPORT_SYMBOL_GPL(spi_unregister_master);
  946. int spi_master_suspend(struct spi_master *master)
  947. {
  948. int ret;
  949. /* Basically no-ops for non-queued masters */
  950. if (!master->queued)
  951. return 0;
  952. ret = spi_stop_queue(master);
  953. if (ret)
  954. dev_err(&master->dev, "queue stop failed\n");
  955. return ret;
  956. }
  957. EXPORT_SYMBOL_GPL(spi_master_suspend);
  958. int spi_master_resume(struct spi_master *master)
  959. {
  960. int ret;
  961. if (!master->queued)
  962. return 0;
  963. ret = spi_start_queue(master);
  964. if (ret)
  965. dev_err(&master->dev, "queue restart failed\n");
  966. return ret;
  967. }
  968. EXPORT_SYMBOL_GPL(spi_master_resume);
  969. static int __spi_master_match(struct device *dev, void *data)
  970. {
  971. struct spi_master *m;
  972. u16 *bus_num = data;
  973. m = container_of(dev, struct spi_master, dev);
  974. return m->bus_num == *bus_num;
  975. }
  976. /**
  977. * spi_busnum_to_master - look up master associated with bus_num
  978. * @bus_num: the master's bus number
  979. * Context: can sleep
  980. *
  981. * This call may be used with devices that are registered after
  982. * arch init time. It returns a refcounted pointer to the relevant
  983. * spi_master (which the caller must release), or NULL if there is
  984. * no such master registered.
  985. */
  986. struct spi_master *spi_busnum_to_master(u16 bus_num)
  987. {
  988. struct device *dev;
  989. struct spi_master *master = NULL;
  990. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  991. __spi_master_match);
  992. if (dev)
  993. master = container_of(dev, struct spi_master, dev);
  994. /* reference got in class_find_device */
  995. return master;
  996. }
  997. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  998. /*-------------------------------------------------------------------------*/
  999. /* Core methods for SPI master protocol drivers. Some of the
  1000. * other core methods are currently defined as inline functions.
  1001. */
  1002. /**
  1003. * spi_setup - setup SPI mode and clock rate
  1004. * @spi: the device whose settings are being modified
  1005. * Context: can sleep, and no requests are queued to the device
  1006. *
  1007. * SPI protocol drivers may need to update the transfer mode if the
  1008. * device doesn't work with its default. They may likewise need
  1009. * to update clock rates or word sizes from initial values. This function
  1010. * changes those settings, and must be called from a context that can sleep.
  1011. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1012. * effect the next time the device is selected and data is transferred to
  1013. * or from it. When this function returns, the spi device is deselected.
  1014. *
  1015. * Note that this call will fail if the protocol driver specifies an option
  1016. * that the underlying controller or its driver does not support. For
  1017. * example, not all hardware supports wire transfers using nine bit words,
  1018. * LSB-first wire encoding, or active-high chipselects.
  1019. */
  1020. int spi_setup(struct spi_device *spi)
  1021. {
  1022. unsigned bad_bits;
  1023. int status = 0;
  1024. /* help drivers fail *cleanly* when they need options
  1025. * that aren't supported with their current master
  1026. */
  1027. bad_bits = spi->mode & ~spi->master->mode_bits;
  1028. if (bad_bits) {
  1029. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1030. bad_bits);
  1031. return -EINVAL;
  1032. }
  1033. if (!spi->bits_per_word)
  1034. spi->bits_per_word = 8;
  1035. if (spi->master->setup)
  1036. status = spi->master->setup(spi);
  1037. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
  1038. "%u bits/w, %u Hz max --> %d\n",
  1039. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1040. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1041. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1042. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1043. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1044. spi->bits_per_word, spi->max_speed_hz,
  1045. status);
  1046. return status;
  1047. }
  1048. EXPORT_SYMBOL_GPL(spi_setup);
  1049. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1050. {
  1051. struct spi_master *master = spi->master;
  1052. struct spi_transfer *xfer;
  1053. /* Half-duplex links include original MicroWire, and ones with
  1054. * only one data pin like SPI_3WIRE (switches direction) or where
  1055. * either MOSI or MISO is missing. They can also be caused by
  1056. * software limitations.
  1057. */
  1058. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1059. || (spi->mode & SPI_3WIRE)) {
  1060. unsigned flags = master->flags;
  1061. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1062. if (xfer->rx_buf && xfer->tx_buf)
  1063. return -EINVAL;
  1064. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1065. return -EINVAL;
  1066. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1067. return -EINVAL;
  1068. }
  1069. }
  1070. /**
  1071. * Set transfer bits_per_word as spi device default if it is not
  1072. * set for this transfer.
  1073. */
  1074. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1075. if (!xfer->bits_per_word)
  1076. xfer->bits_per_word = spi->bits_per_word;
  1077. }
  1078. message->spi = spi;
  1079. message->status = -EINPROGRESS;
  1080. return master->transfer(spi, message);
  1081. }
  1082. /**
  1083. * spi_async - asynchronous SPI transfer
  1084. * @spi: device with which data will be exchanged
  1085. * @message: describes the data transfers, including completion callback
  1086. * Context: any (irqs may be blocked, etc)
  1087. *
  1088. * This call may be used in_irq and other contexts which can't sleep,
  1089. * as well as from task contexts which can sleep.
  1090. *
  1091. * The completion callback is invoked in a context which can't sleep.
  1092. * Before that invocation, the value of message->status is undefined.
  1093. * When the callback is issued, message->status holds either zero (to
  1094. * indicate complete success) or a negative error code. After that
  1095. * callback returns, the driver which issued the transfer request may
  1096. * deallocate the associated memory; it's no longer in use by any SPI
  1097. * core or controller driver code.
  1098. *
  1099. * Note that although all messages to a spi_device are handled in
  1100. * FIFO order, messages may go to different devices in other orders.
  1101. * Some device might be higher priority, or have various "hard" access
  1102. * time requirements, for example.
  1103. *
  1104. * On detection of any fault during the transfer, processing of
  1105. * the entire message is aborted, and the device is deselected.
  1106. * Until returning from the associated message completion callback,
  1107. * no other spi_message queued to that device will be processed.
  1108. * (This rule applies equally to all the synchronous transfer calls,
  1109. * which are wrappers around this core asynchronous primitive.)
  1110. */
  1111. int spi_async(struct spi_device *spi, struct spi_message *message)
  1112. {
  1113. struct spi_master *master = spi->master;
  1114. int ret;
  1115. unsigned long flags;
  1116. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1117. if (master->bus_lock_flag)
  1118. ret = -EBUSY;
  1119. else
  1120. ret = __spi_async(spi, message);
  1121. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1122. return ret;
  1123. }
  1124. EXPORT_SYMBOL_GPL(spi_async);
  1125. /**
  1126. * spi_async_locked - version of spi_async with exclusive bus usage
  1127. * @spi: device with which data will be exchanged
  1128. * @message: describes the data transfers, including completion callback
  1129. * Context: any (irqs may be blocked, etc)
  1130. *
  1131. * This call may be used in_irq and other contexts which can't sleep,
  1132. * as well as from task contexts which can sleep.
  1133. *
  1134. * The completion callback is invoked in a context which can't sleep.
  1135. * Before that invocation, the value of message->status is undefined.
  1136. * When the callback is issued, message->status holds either zero (to
  1137. * indicate complete success) or a negative error code. After that
  1138. * callback returns, the driver which issued the transfer request may
  1139. * deallocate the associated memory; it's no longer in use by any SPI
  1140. * core or controller driver code.
  1141. *
  1142. * Note that although all messages to a spi_device are handled in
  1143. * FIFO order, messages may go to different devices in other orders.
  1144. * Some device might be higher priority, or have various "hard" access
  1145. * time requirements, for example.
  1146. *
  1147. * On detection of any fault during the transfer, processing of
  1148. * the entire message is aborted, and the device is deselected.
  1149. * Until returning from the associated message completion callback,
  1150. * no other spi_message queued to that device will be processed.
  1151. * (This rule applies equally to all the synchronous transfer calls,
  1152. * which are wrappers around this core asynchronous primitive.)
  1153. */
  1154. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1155. {
  1156. struct spi_master *master = spi->master;
  1157. int ret;
  1158. unsigned long flags;
  1159. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1160. ret = __spi_async(spi, message);
  1161. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1162. return ret;
  1163. }
  1164. EXPORT_SYMBOL_GPL(spi_async_locked);
  1165. /*-------------------------------------------------------------------------*/
  1166. /* Utility methods for SPI master protocol drivers, layered on
  1167. * top of the core. Some other utility methods are defined as
  1168. * inline functions.
  1169. */
  1170. static void spi_complete(void *arg)
  1171. {
  1172. complete(arg);
  1173. }
  1174. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1175. int bus_locked)
  1176. {
  1177. DECLARE_COMPLETION_ONSTACK(done);
  1178. int status;
  1179. struct spi_master *master = spi->master;
  1180. message->complete = spi_complete;
  1181. message->context = &done;
  1182. if (!bus_locked)
  1183. mutex_lock(&master->bus_lock_mutex);
  1184. status = spi_async_locked(spi, message);
  1185. if (!bus_locked)
  1186. mutex_unlock(&master->bus_lock_mutex);
  1187. if (status == 0) {
  1188. wait_for_completion(&done);
  1189. status = message->status;
  1190. }
  1191. message->context = NULL;
  1192. return status;
  1193. }
  1194. /**
  1195. * spi_sync - blocking/synchronous SPI data transfers
  1196. * @spi: device with which data will be exchanged
  1197. * @message: describes the data transfers
  1198. * Context: can sleep
  1199. *
  1200. * This call may only be used from a context that may sleep. The sleep
  1201. * is non-interruptible, and has no timeout. Low-overhead controller
  1202. * drivers may DMA directly into and out of the message buffers.
  1203. *
  1204. * Note that the SPI device's chip select is active during the message,
  1205. * and then is normally disabled between messages. Drivers for some
  1206. * frequently-used devices may want to minimize costs of selecting a chip,
  1207. * by leaving it selected in anticipation that the next message will go
  1208. * to the same chip. (That may increase power usage.)
  1209. *
  1210. * Also, the caller is guaranteeing that the memory associated with the
  1211. * message will not be freed before this call returns.
  1212. *
  1213. * It returns zero on success, else a negative error code.
  1214. */
  1215. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1216. {
  1217. return __spi_sync(spi, message, 0);
  1218. }
  1219. EXPORT_SYMBOL_GPL(spi_sync);
  1220. /**
  1221. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1222. * @spi: device with which data will be exchanged
  1223. * @message: describes the data transfers
  1224. * Context: can sleep
  1225. *
  1226. * This call may only be used from a context that may sleep. The sleep
  1227. * is non-interruptible, and has no timeout. Low-overhead controller
  1228. * drivers may DMA directly into and out of the message buffers.
  1229. *
  1230. * This call should be used by drivers that require exclusive access to the
  1231. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1232. * be released by a spi_bus_unlock call when the exclusive access is over.
  1233. *
  1234. * It returns zero on success, else a negative error code.
  1235. */
  1236. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1237. {
  1238. return __spi_sync(spi, message, 1);
  1239. }
  1240. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1241. /**
  1242. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1243. * @master: SPI bus master that should be locked for exclusive bus access
  1244. * Context: can sleep
  1245. *
  1246. * This call may only be used from a context that may sleep. The sleep
  1247. * is non-interruptible, and has no timeout.
  1248. *
  1249. * This call should be used by drivers that require exclusive access to the
  1250. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1251. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1252. * and spi_async_locked calls when the SPI bus lock is held.
  1253. *
  1254. * It returns zero on success, else a negative error code.
  1255. */
  1256. int spi_bus_lock(struct spi_master *master)
  1257. {
  1258. unsigned long flags;
  1259. mutex_lock(&master->bus_lock_mutex);
  1260. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1261. master->bus_lock_flag = 1;
  1262. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1263. /* mutex remains locked until spi_bus_unlock is called */
  1264. return 0;
  1265. }
  1266. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1267. /**
  1268. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1269. * @master: SPI bus master that was locked for exclusive bus access
  1270. * Context: can sleep
  1271. *
  1272. * This call may only be used from a context that may sleep. The sleep
  1273. * is non-interruptible, and has no timeout.
  1274. *
  1275. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1276. * call.
  1277. *
  1278. * It returns zero on success, else a negative error code.
  1279. */
  1280. int spi_bus_unlock(struct spi_master *master)
  1281. {
  1282. master->bus_lock_flag = 0;
  1283. mutex_unlock(&master->bus_lock_mutex);
  1284. return 0;
  1285. }
  1286. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1287. /* portable code must never pass more than 32 bytes */
  1288. #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
  1289. static u8 *buf;
  1290. /**
  1291. * spi_write_then_read - SPI synchronous write followed by read
  1292. * @spi: device with which data will be exchanged
  1293. * @txbuf: data to be written (need not be dma-safe)
  1294. * @n_tx: size of txbuf, in bytes
  1295. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1296. * @n_rx: size of rxbuf, in bytes
  1297. * Context: can sleep
  1298. *
  1299. * This performs a half duplex MicroWire style transaction with the
  1300. * device, sending txbuf and then reading rxbuf. The return value
  1301. * is zero for success, else a negative errno status code.
  1302. * This call may only be used from a context that may sleep.
  1303. *
  1304. * Parameters to this routine are always copied using a small buffer;
  1305. * portable code should never use this for more than 32 bytes.
  1306. * Performance-sensitive or bulk transfer code should instead use
  1307. * spi_{async,sync}() calls with dma-safe buffers.
  1308. */
  1309. int spi_write_then_read(struct spi_device *spi,
  1310. const void *txbuf, unsigned n_tx,
  1311. void *rxbuf, unsigned n_rx)
  1312. {
  1313. static DEFINE_MUTEX(lock);
  1314. int status;
  1315. struct spi_message message;
  1316. struct spi_transfer x[2];
  1317. u8 *local_buf;
  1318. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1319. * copying here, (as a pure convenience thing), but we can
  1320. * keep heap costs out of the hot path unless someone else is
  1321. * using the pre-allocated buffer or the transfer is too large.
  1322. */
  1323. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1324. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), GFP_KERNEL);
  1325. if (!local_buf)
  1326. return -ENOMEM;
  1327. } else {
  1328. local_buf = buf;
  1329. }
  1330. spi_message_init(&message);
  1331. memset(x, 0, sizeof x);
  1332. if (n_tx) {
  1333. x[0].len = n_tx;
  1334. spi_message_add_tail(&x[0], &message);
  1335. }
  1336. if (n_rx) {
  1337. x[1].len = n_rx;
  1338. spi_message_add_tail(&x[1], &message);
  1339. }
  1340. memcpy(local_buf, txbuf, n_tx);
  1341. x[0].tx_buf = local_buf;
  1342. x[1].rx_buf = local_buf + n_tx;
  1343. /* do the i/o */
  1344. status = spi_sync(spi, &message);
  1345. if (status == 0)
  1346. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1347. if (x[0].tx_buf == buf)
  1348. mutex_unlock(&lock);
  1349. else
  1350. kfree(local_buf);
  1351. return status;
  1352. }
  1353. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1354. /*-------------------------------------------------------------------------*/
  1355. static int __init spi_init(void)
  1356. {
  1357. int status;
  1358. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1359. if (!buf) {
  1360. status = -ENOMEM;
  1361. goto err0;
  1362. }
  1363. status = bus_register(&spi_bus_type);
  1364. if (status < 0)
  1365. goto err1;
  1366. status = class_register(&spi_master_class);
  1367. if (status < 0)
  1368. goto err2;
  1369. return 0;
  1370. err2:
  1371. bus_unregister(&spi_bus_type);
  1372. err1:
  1373. kfree(buf);
  1374. buf = NULL;
  1375. err0:
  1376. return status;
  1377. }
  1378. /* board_info is normally registered in arch_initcall(),
  1379. * but even essential drivers wait till later
  1380. *
  1381. * REVISIT only boardinfo really needs static linking. the rest (device and
  1382. * driver registration) _could_ be dynamically linked (modular) ... costs
  1383. * include needing to have boardinfo data structures be much more public.
  1384. */
  1385. postcore_initcall(spi_init);