spi.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 __init
  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. dev_err(&master->dev,
  458. "failed to unprepare transfer hardware\n");
  459. return;
  460. }
  461. }
  462. master->busy = false;
  463. spin_unlock_irqrestore(&master->queue_lock, flags);
  464. return;
  465. }
  466. /* Make sure we are not already running a message */
  467. if (master->cur_msg) {
  468. spin_unlock_irqrestore(&master->queue_lock, flags);
  469. return;
  470. }
  471. /* Extract head of queue */
  472. master->cur_msg =
  473. list_entry(master->queue.next, struct spi_message, queue);
  474. list_del_init(&master->cur_msg->queue);
  475. if (master->busy)
  476. was_busy = true;
  477. else
  478. master->busy = true;
  479. spin_unlock_irqrestore(&master->queue_lock, flags);
  480. if (!was_busy) {
  481. ret = master->prepare_transfer_hardware(master);
  482. if (ret) {
  483. dev_err(&master->dev,
  484. "failed to prepare transfer hardware\n");
  485. return;
  486. }
  487. }
  488. ret = master->transfer_one_message(master, master->cur_msg);
  489. if (ret) {
  490. dev_err(&master->dev,
  491. "failed to transfer one message from queue\n");
  492. return;
  493. }
  494. }
  495. static int spi_init_queue(struct spi_master *master)
  496. {
  497. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  498. INIT_LIST_HEAD(&master->queue);
  499. spin_lock_init(&master->queue_lock);
  500. master->running = false;
  501. master->busy = false;
  502. init_kthread_worker(&master->kworker);
  503. master->kworker_task = kthread_run(kthread_worker_fn,
  504. &master->kworker,
  505. dev_name(&master->dev));
  506. if (IS_ERR(master->kworker_task)) {
  507. dev_err(&master->dev, "failed to create message pump task\n");
  508. return -ENOMEM;
  509. }
  510. init_kthread_work(&master->pump_messages, spi_pump_messages);
  511. /*
  512. * Master config will indicate if this controller should run the
  513. * message pump with high (realtime) priority to reduce the transfer
  514. * latency on the bus by minimising the delay between a transfer
  515. * request and the scheduling of the message pump thread. Without this
  516. * setting the message pump thread will remain at default priority.
  517. */
  518. if (master->rt) {
  519. dev_info(&master->dev,
  520. "will run message pump with realtime priority\n");
  521. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  522. }
  523. return 0;
  524. }
  525. /**
  526. * spi_get_next_queued_message() - called by driver to check for queued
  527. * messages
  528. * @master: the master to check for queued messages
  529. *
  530. * If there are more messages in the queue, the next message is returned from
  531. * this call.
  532. */
  533. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  534. {
  535. struct spi_message *next;
  536. unsigned long flags;
  537. /* get a pointer to the next message, if any */
  538. spin_lock_irqsave(&master->queue_lock, flags);
  539. if (list_empty(&master->queue))
  540. next = NULL;
  541. else
  542. next = list_entry(master->queue.next,
  543. struct spi_message, queue);
  544. spin_unlock_irqrestore(&master->queue_lock, flags);
  545. return next;
  546. }
  547. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  548. /**
  549. * spi_finalize_current_message() - the current message is complete
  550. * @master: the master to return the message to
  551. *
  552. * Called by the driver to notify the core that the message in the front of the
  553. * queue is complete and can be removed from the queue.
  554. */
  555. void spi_finalize_current_message(struct spi_master *master)
  556. {
  557. struct spi_message *mesg;
  558. unsigned long flags;
  559. spin_lock_irqsave(&master->queue_lock, flags);
  560. mesg = master->cur_msg;
  561. master->cur_msg = NULL;
  562. queue_kthread_work(&master->kworker, &master->pump_messages);
  563. spin_unlock_irqrestore(&master->queue_lock, flags);
  564. mesg->state = NULL;
  565. if (mesg->complete)
  566. mesg->complete(mesg->context);
  567. }
  568. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  569. static int spi_start_queue(struct spi_master *master)
  570. {
  571. unsigned long flags;
  572. spin_lock_irqsave(&master->queue_lock, flags);
  573. if (master->running || master->busy) {
  574. spin_unlock_irqrestore(&master->queue_lock, flags);
  575. return -EBUSY;
  576. }
  577. master->running = true;
  578. master->cur_msg = NULL;
  579. spin_unlock_irqrestore(&master->queue_lock, flags);
  580. queue_kthread_work(&master->kworker, &master->pump_messages);
  581. return 0;
  582. }
  583. static int spi_stop_queue(struct spi_master *master)
  584. {
  585. unsigned long flags;
  586. unsigned limit = 500;
  587. int ret = 0;
  588. spin_lock_irqsave(&master->queue_lock, flags);
  589. /*
  590. * This is a bit lame, but is optimized for the common execution path.
  591. * A wait_queue on the master->busy could be used, but then the common
  592. * execution path (pump_messages) would be required to call wake_up or
  593. * friends on every SPI message. Do this instead.
  594. */
  595. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  596. spin_unlock_irqrestore(&master->queue_lock, flags);
  597. msleep(10);
  598. spin_lock_irqsave(&master->queue_lock, flags);
  599. }
  600. if (!list_empty(&master->queue) || master->busy)
  601. ret = -EBUSY;
  602. else
  603. master->running = false;
  604. spin_unlock_irqrestore(&master->queue_lock, flags);
  605. if (ret) {
  606. dev_warn(&master->dev,
  607. "could not stop message queue\n");
  608. return ret;
  609. }
  610. return ret;
  611. }
  612. static int spi_destroy_queue(struct spi_master *master)
  613. {
  614. int ret;
  615. ret = spi_stop_queue(master);
  616. /*
  617. * flush_kthread_worker will block until all work is done.
  618. * If the reason that stop_queue timed out is that the work will never
  619. * finish, then it does no good to call flush/stop thread, so
  620. * return anyway.
  621. */
  622. if (ret) {
  623. dev_err(&master->dev, "problem destroying queue\n");
  624. return ret;
  625. }
  626. flush_kthread_worker(&master->kworker);
  627. kthread_stop(master->kworker_task);
  628. return 0;
  629. }
  630. /**
  631. * spi_queued_transfer - transfer function for queued transfers
  632. * @spi: spi device which is requesting transfer
  633. * @msg: spi message which is to handled is queued to driver queue
  634. */
  635. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  636. {
  637. struct spi_master *master = spi->master;
  638. unsigned long flags;
  639. spin_lock_irqsave(&master->queue_lock, flags);
  640. if (!master->running) {
  641. spin_unlock_irqrestore(&master->queue_lock, flags);
  642. return -ESHUTDOWN;
  643. }
  644. msg->actual_length = 0;
  645. msg->status = -EINPROGRESS;
  646. list_add_tail(&msg->queue, &master->queue);
  647. if (master->running && !master->busy)
  648. queue_kthread_work(&master->kworker, &master->pump_messages);
  649. spin_unlock_irqrestore(&master->queue_lock, flags);
  650. return 0;
  651. }
  652. static int spi_master_initialize_queue(struct spi_master *master)
  653. {
  654. int ret;
  655. master->queued = true;
  656. master->transfer = spi_queued_transfer;
  657. /* Initialize and start queue */
  658. ret = spi_init_queue(master);
  659. if (ret) {
  660. dev_err(&master->dev, "problem initializing queue\n");
  661. goto err_init_queue;
  662. }
  663. ret = spi_start_queue(master);
  664. if (ret) {
  665. dev_err(&master->dev, "problem starting queue\n");
  666. goto err_start_queue;
  667. }
  668. return 0;
  669. err_start_queue:
  670. err_init_queue:
  671. spi_destroy_queue(master);
  672. return ret;
  673. }
  674. /*-------------------------------------------------------------------------*/
  675. static void spi_master_release(struct device *dev)
  676. {
  677. struct spi_master *master;
  678. master = container_of(dev, struct spi_master, dev);
  679. kfree(master);
  680. }
  681. static struct class spi_master_class = {
  682. .name = "spi_master",
  683. .owner = THIS_MODULE,
  684. .dev_release = spi_master_release,
  685. };
  686. /**
  687. * spi_alloc_master - allocate SPI master controller
  688. * @dev: the controller, possibly using the platform_bus
  689. * @size: how much zeroed driver-private data to allocate; the pointer to this
  690. * memory is in the driver_data field of the returned device,
  691. * accessible with spi_master_get_devdata().
  692. * Context: can sleep
  693. *
  694. * This call is used only by SPI master controller drivers, which are the
  695. * only ones directly touching chip registers. It's how they allocate
  696. * an spi_master structure, prior to calling spi_register_master().
  697. *
  698. * This must be called from context that can sleep. It returns the SPI
  699. * master structure on success, else NULL.
  700. *
  701. * The caller is responsible for assigning the bus number and initializing
  702. * the master's methods before calling spi_register_master(); and (after errors
  703. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  704. * 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 = get_device(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. status = 0;
  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);