spi.c 43 KB

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