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