spi.c 47 KB

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