spi.c 43 KB

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