spi.c 44 KB

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