spi.c 41 KB

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