spi.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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, "%s",
  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 DUAL/QUAD mode */
  740. prop = of_get_property(nc, "spi-tx-bus-width", &len);
  741. if (prop && len == sizeof(*prop)) {
  742. switch (be32_to_cpup(prop)) {
  743. case SPI_NBITS_SINGLE:
  744. break;
  745. case SPI_NBITS_DUAL:
  746. spi->mode |= SPI_TX_DUAL;
  747. break;
  748. case SPI_NBITS_QUAD:
  749. spi->mode |= SPI_TX_QUAD;
  750. break;
  751. default:
  752. dev_err(&master->dev,
  753. "spi-tx-bus-width %d not supported\n",
  754. be32_to_cpup(prop));
  755. spi_dev_put(spi);
  756. continue;
  757. }
  758. }
  759. prop = of_get_property(nc, "spi-rx-bus-width", &len);
  760. if (prop && len == sizeof(*prop)) {
  761. switch (be32_to_cpup(prop)) {
  762. case SPI_NBITS_SINGLE:
  763. break;
  764. case SPI_NBITS_DUAL:
  765. spi->mode |= SPI_RX_DUAL;
  766. break;
  767. case SPI_NBITS_QUAD:
  768. spi->mode |= SPI_RX_QUAD;
  769. break;
  770. default:
  771. dev_err(&master->dev,
  772. "spi-rx-bus-width %d not supported\n",
  773. be32_to_cpup(prop));
  774. spi_dev_put(spi);
  775. continue;
  776. }
  777. }
  778. /* Device speed */
  779. prop = of_get_property(nc, "spi-max-frequency", &len);
  780. if (!prop || len < sizeof(*prop)) {
  781. dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
  782. nc->full_name);
  783. spi_dev_put(spi);
  784. continue;
  785. }
  786. spi->max_speed_hz = be32_to_cpup(prop);
  787. /* IRQ */
  788. spi->irq = irq_of_parse_and_map(nc, 0);
  789. /* Store a pointer to the node in the device structure */
  790. of_node_get(nc);
  791. spi->dev.of_node = nc;
  792. /* Register the new device */
  793. snprintf(modalias, sizeof(modalias), "%s%s", SPI_MODULE_PREFIX,
  794. spi->modalias);
  795. request_module(modalias);
  796. rc = spi_add_device(spi);
  797. if (rc) {
  798. dev_err(&master->dev, "spi_device register error %s\n",
  799. nc->full_name);
  800. spi_dev_put(spi);
  801. }
  802. }
  803. }
  804. #else
  805. static void of_register_spi_devices(struct spi_master *master) { }
  806. #endif
  807. #ifdef CONFIG_ACPI
  808. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  809. {
  810. struct spi_device *spi = data;
  811. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  812. struct acpi_resource_spi_serialbus *sb;
  813. sb = &ares->data.spi_serial_bus;
  814. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  815. spi->chip_select = sb->device_selection;
  816. spi->max_speed_hz = sb->connection_speed;
  817. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  818. spi->mode |= SPI_CPHA;
  819. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  820. spi->mode |= SPI_CPOL;
  821. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  822. spi->mode |= SPI_CS_HIGH;
  823. }
  824. } else if (spi->irq < 0) {
  825. struct resource r;
  826. if (acpi_dev_resource_interrupt(ares, 0, &r))
  827. spi->irq = r.start;
  828. }
  829. /* Always tell the ACPI core to skip this resource */
  830. return 1;
  831. }
  832. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  833. void *data, void **return_value)
  834. {
  835. struct spi_master *master = data;
  836. struct list_head resource_list;
  837. struct acpi_device *adev;
  838. struct spi_device *spi;
  839. int ret;
  840. if (acpi_bus_get_device(handle, &adev))
  841. return AE_OK;
  842. if (acpi_bus_get_status(adev) || !adev->status.present)
  843. return AE_OK;
  844. spi = spi_alloc_device(master);
  845. if (!spi) {
  846. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  847. dev_name(&adev->dev));
  848. return AE_NO_MEMORY;
  849. }
  850. ACPI_HANDLE_SET(&spi->dev, handle);
  851. spi->irq = -1;
  852. INIT_LIST_HEAD(&resource_list);
  853. ret = acpi_dev_get_resources(adev, &resource_list,
  854. acpi_spi_add_resource, spi);
  855. acpi_dev_free_resource_list(&resource_list);
  856. if (ret < 0 || !spi->max_speed_hz) {
  857. spi_dev_put(spi);
  858. return AE_OK;
  859. }
  860. strlcpy(spi->modalias, dev_name(&adev->dev), sizeof(spi->modalias));
  861. if (spi_add_device(spi)) {
  862. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  863. dev_name(&adev->dev));
  864. spi_dev_put(spi);
  865. }
  866. return AE_OK;
  867. }
  868. static void acpi_register_spi_devices(struct spi_master *master)
  869. {
  870. acpi_status status;
  871. acpi_handle handle;
  872. handle = ACPI_HANDLE(master->dev.parent);
  873. if (!handle)
  874. return;
  875. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  876. acpi_spi_add_device, NULL,
  877. master, NULL);
  878. if (ACPI_FAILURE(status))
  879. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  880. }
  881. #else
  882. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  883. #endif /* CONFIG_ACPI */
  884. static void spi_master_release(struct device *dev)
  885. {
  886. struct spi_master *master;
  887. master = container_of(dev, struct spi_master, dev);
  888. kfree(master);
  889. }
  890. static struct class spi_master_class = {
  891. .name = "spi_master",
  892. .owner = THIS_MODULE,
  893. .dev_release = spi_master_release,
  894. };
  895. /**
  896. * spi_alloc_master - allocate SPI master controller
  897. * @dev: the controller, possibly using the platform_bus
  898. * @size: how much zeroed driver-private data to allocate; the pointer to this
  899. * memory is in the driver_data field of the returned device,
  900. * accessible with spi_master_get_devdata().
  901. * Context: can sleep
  902. *
  903. * This call is used only by SPI master controller drivers, which are the
  904. * only ones directly touching chip registers. It's how they allocate
  905. * an spi_master structure, prior to calling spi_register_master().
  906. *
  907. * This must be called from context that can sleep. It returns the SPI
  908. * master structure on success, else NULL.
  909. *
  910. * The caller is responsible for assigning the bus number and initializing
  911. * the master's methods before calling spi_register_master(); and (after errors
  912. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  913. * leak.
  914. */
  915. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  916. {
  917. struct spi_master *master;
  918. if (!dev)
  919. return NULL;
  920. master = kzalloc(size + sizeof *master, GFP_KERNEL);
  921. if (!master)
  922. return NULL;
  923. device_initialize(&master->dev);
  924. master->bus_num = -1;
  925. master->num_chipselect = 1;
  926. master->dev.class = &spi_master_class;
  927. master->dev.parent = get_device(dev);
  928. spi_master_set_devdata(master, &master[1]);
  929. return master;
  930. }
  931. EXPORT_SYMBOL_GPL(spi_alloc_master);
  932. #ifdef CONFIG_OF
  933. static int of_spi_register_master(struct spi_master *master)
  934. {
  935. int nb, i, *cs;
  936. struct device_node *np = master->dev.of_node;
  937. if (!np)
  938. return 0;
  939. nb = of_gpio_named_count(np, "cs-gpios");
  940. master->num_chipselect = max(nb, (int)master->num_chipselect);
  941. /* Return error only for an incorrectly formed cs-gpios property */
  942. if (nb == 0 || nb == -ENOENT)
  943. return 0;
  944. else if (nb < 0)
  945. return nb;
  946. cs = devm_kzalloc(&master->dev,
  947. sizeof(int) * master->num_chipselect,
  948. GFP_KERNEL);
  949. master->cs_gpios = cs;
  950. if (!master->cs_gpios)
  951. return -ENOMEM;
  952. for (i = 0; i < master->num_chipselect; i++)
  953. cs[i] = -ENOENT;
  954. for (i = 0; i < nb; i++)
  955. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  956. return 0;
  957. }
  958. #else
  959. static int of_spi_register_master(struct spi_master *master)
  960. {
  961. return 0;
  962. }
  963. #endif
  964. /**
  965. * spi_register_master - register SPI master controller
  966. * @master: initialized master, originally from spi_alloc_master()
  967. * Context: can sleep
  968. *
  969. * SPI master controllers connect to their drivers using some non-SPI bus,
  970. * such as the platform bus. The final stage of probe() in that code
  971. * includes calling spi_register_master() to hook up to this SPI bus glue.
  972. *
  973. * SPI controllers use board specific (often SOC specific) bus numbers,
  974. * and board-specific addressing for SPI devices combines those numbers
  975. * with chip select numbers. Since SPI does not directly support dynamic
  976. * device identification, boards need configuration tables telling which
  977. * chip is at which address.
  978. *
  979. * This must be called from context that can sleep. It returns zero on
  980. * success, else a negative error code (dropping the master's refcount).
  981. * After a successful return, the caller is responsible for calling
  982. * spi_unregister_master().
  983. */
  984. int spi_register_master(struct spi_master *master)
  985. {
  986. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  987. struct device *dev = master->dev.parent;
  988. struct boardinfo *bi;
  989. int status = -ENODEV;
  990. int dynamic = 0;
  991. if (!dev)
  992. return -ENODEV;
  993. status = of_spi_register_master(master);
  994. if (status)
  995. return status;
  996. /* even if it's just one always-selected device, there must
  997. * be at least one chipselect
  998. */
  999. if (master->num_chipselect == 0)
  1000. return -EINVAL;
  1001. if ((master->bus_num < 0) && master->dev.of_node)
  1002. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1003. /* convention: dynamically assigned bus IDs count down from the max */
  1004. if (master->bus_num < 0) {
  1005. /* FIXME switch to an IDR based scheme, something like
  1006. * I2C now uses, so we can't run out of "dynamic" IDs
  1007. */
  1008. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1009. dynamic = 1;
  1010. }
  1011. spin_lock_init(&master->bus_lock_spinlock);
  1012. mutex_init(&master->bus_lock_mutex);
  1013. master->bus_lock_flag = 0;
  1014. /* register the device, then userspace will see it.
  1015. * registration fails if the bus ID is in use.
  1016. */
  1017. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1018. status = device_add(&master->dev);
  1019. if (status < 0)
  1020. goto done;
  1021. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1022. dynamic ? " (dynamic)" : "");
  1023. /* If we're using a queued driver, start the queue */
  1024. if (master->transfer)
  1025. dev_info(dev, "master is unqueued, this is deprecated\n");
  1026. else {
  1027. status = spi_master_initialize_queue(master);
  1028. if (status) {
  1029. device_unregister(&master->dev);
  1030. goto done;
  1031. }
  1032. }
  1033. mutex_lock(&board_lock);
  1034. list_add_tail(&master->list, &spi_master_list);
  1035. list_for_each_entry(bi, &board_list, list)
  1036. spi_match_master_to_boardinfo(master, &bi->board_info);
  1037. mutex_unlock(&board_lock);
  1038. /* Register devices from the device tree and ACPI */
  1039. of_register_spi_devices(master);
  1040. acpi_register_spi_devices(master);
  1041. done:
  1042. return status;
  1043. }
  1044. EXPORT_SYMBOL_GPL(spi_register_master);
  1045. static int __unregister(struct device *dev, void *null)
  1046. {
  1047. spi_unregister_device(to_spi_device(dev));
  1048. return 0;
  1049. }
  1050. /**
  1051. * spi_unregister_master - unregister SPI master controller
  1052. * @master: the master being unregistered
  1053. * Context: can sleep
  1054. *
  1055. * This call is used only by SPI master controller drivers, which are the
  1056. * only ones directly touching chip registers.
  1057. *
  1058. * This must be called from context that can sleep.
  1059. */
  1060. void spi_unregister_master(struct spi_master *master)
  1061. {
  1062. int dummy;
  1063. if (master->queued) {
  1064. if (spi_destroy_queue(master))
  1065. dev_err(&master->dev, "queue remove failed\n");
  1066. }
  1067. mutex_lock(&board_lock);
  1068. list_del(&master->list);
  1069. mutex_unlock(&board_lock);
  1070. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1071. device_unregister(&master->dev);
  1072. }
  1073. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1074. int spi_master_suspend(struct spi_master *master)
  1075. {
  1076. int ret;
  1077. /* Basically no-ops for non-queued masters */
  1078. if (!master->queued)
  1079. return 0;
  1080. ret = spi_stop_queue(master);
  1081. if (ret)
  1082. dev_err(&master->dev, "queue stop failed\n");
  1083. return ret;
  1084. }
  1085. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1086. int spi_master_resume(struct spi_master *master)
  1087. {
  1088. int ret;
  1089. if (!master->queued)
  1090. return 0;
  1091. ret = spi_start_queue(master);
  1092. if (ret)
  1093. dev_err(&master->dev, "queue restart failed\n");
  1094. return ret;
  1095. }
  1096. EXPORT_SYMBOL_GPL(spi_master_resume);
  1097. static int __spi_master_match(struct device *dev, const void *data)
  1098. {
  1099. struct spi_master *m;
  1100. const u16 *bus_num = data;
  1101. m = container_of(dev, struct spi_master, dev);
  1102. return m->bus_num == *bus_num;
  1103. }
  1104. /**
  1105. * spi_busnum_to_master - look up master associated with bus_num
  1106. * @bus_num: the master's bus number
  1107. * Context: can sleep
  1108. *
  1109. * This call may be used with devices that are registered after
  1110. * arch init time. It returns a refcounted pointer to the relevant
  1111. * spi_master (which the caller must release), or NULL if there is
  1112. * no such master registered.
  1113. */
  1114. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1115. {
  1116. struct device *dev;
  1117. struct spi_master *master = NULL;
  1118. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1119. __spi_master_match);
  1120. if (dev)
  1121. master = container_of(dev, struct spi_master, dev);
  1122. /* reference got in class_find_device */
  1123. return master;
  1124. }
  1125. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1126. /*-------------------------------------------------------------------------*/
  1127. /* Core methods for SPI master protocol drivers. Some of the
  1128. * other core methods are currently defined as inline functions.
  1129. */
  1130. /**
  1131. * spi_setup - setup SPI mode and clock rate
  1132. * @spi: the device whose settings are being modified
  1133. * Context: can sleep, and no requests are queued to the device
  1134. *
  1135. * SPI protocol drivers may need to update the transfer mode if the
  1136. * device doesn't work with its default. They may likewise need
  1137. * to update clock rates or word sizes from initial values. This function
  1138. * changes those settings, and must be called from a context that can sleep.
  1139. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1140. * effect the next time the device is selected and data is transferred to
  1141. * or from it. When this function returns, the spi device is deselected.
  1142. *
  1143. * Note that this call will fail if the protocol driver specifies an option
  1144. * that the underlying controller or its driver does not support. For
  1145. * example, not all hardware supports wire transfers using nine bit words,
  1146. * LSB-first wire encoding, or active-high chipselects.
  1147. */
  1148. int spi_setup(struct spi_device *spi)
  1149. {
  1150. unsigned bad_bits;
  1151. int status = 0;
  1152. /* check mode to prevent that DUAL and QUAD set at the same time
  1153. */
  1154. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1155. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1156. dev_err(&spi->dev,
  1157. "setup: can not select dual and quad at the same time\n");
  1158. return -EINVAL;
  1159. }
  1160. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1161. */
  1162. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1163. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1164. return -EINVAL;
  1165. /* help drivers fail *cleanly* when they need options
  1166. * that aren't supported with their current master
  1167. */
  1168. bad_bits = spi->mode & ~spi->master->mode_bits;
  1169. if (bad_bits) {
  1170. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1171. bad_bits);
  1172. return -EINVAL;
  1173. }
  1174. if (!spi->bits_per_word)
  1175. spi->bits_per_word = 8;
  1176. if (spi->master->setup)
  1177. status = spi->master->setup(spi);
  1178. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
  1179. "%u bits/w, %u Hz max --> %d\n",
  1180. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1181. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1182. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1183. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1184. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1185. spi->bits_per_word, spi->max_speed_hz,
  1186. status);
  1187. return status;
  1188. }
  1189. EXPORT_SYMBOL_GPL(spi_setup);
  1190. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1191. {
  1192. struct spi_master *master = spi->master;
  1193. struct spi_transfer *xfer;
  1194. if (list_empty(&message->transfers))
  1195. return -EINVAL;
  1196. if (!message->complete)
  1197. return -EINVAL;
  1198. /* Half-duplex links include original MicroWire, and ones with
  1199. * only one data pin like SPI_3WIRE (switches direction) or where
  1200. * either MOSI or MISO is missing. They can also be caused by
  1201. * software limitations.
  1202. */
  1203. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1204. || (spi->mode & SPI_3WIRE)) {
  1205. unsigned flags = master->flags;
  1206. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1207. if (xfer->rx_buf && xfer->tx_buf)
  1208. return -EINVAL;
  1209. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1210. return -EINVAL;
  1211. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1212. return -EINVAL;
  1213. }
  1214. }
  1215. /**
  1216. * Set transfer bits_per_word and max speed as spi device default if
  1217. * it is not set for this transfer.
  1218. * Set transfer tx_nbits and rx_nbits as single transfer default
  1219. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1220. */
  1221. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1222. if (!xfer->bits_per_word)
  1223. xfer->bits_per_word = spi->bits_per_word;
  1224. if (!xfer->speed_hz) {
  1225. xfer->speed_hz = spi->max_speed_hz;
  1226. if (master->max_speed_hz &&
  1227. xfer->speed_hz > master->max_speed_hz)
  1228. xfer->speed_hz = master->max_speed_hz;
  1229. }
  1230. if (master->bits_per_word_mask) {
  1231. /* Only 32 bits fit in the mask */
  1232. if (xfer->bits_per_word > 32)
  1233. return -EINVAL;
  1234. if (!(master->bits_per_word_mask &
  1235. BIT(xfer->bits_per_word - 1)))
  1236. return -EINVAL;
  1237. }
  1238. if (xfer->speed_hz && master->min_speed_hz &&
  1239. xfer->speed_hz < master->min_speed_hz)
  1240. return -EINVAL;
  1241. if (xfer->speed_hz && master->max_speed_hz &&
  1242. xfer->speed_hz > master->max_speed_hz)
  1243. return -EINVAL;
  1244. if (xfer->tx_buf && !xfer->tx_nbits)
  1245. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1246. if (xfer->rx_buf && !xfer->rx_nbits)
  1247. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1248. /* check transfer tx/rx_nbits:
  1249. * 1. keep the value is not out of single, dual and quad
  1250. * 2. keep tx/rx_nbits is contained by mode in spi_device
  1251. * 3. if SPI_3WIRE, tx/rx_nbits should be in single
  1252. */
  1253. if (xfer->tx_buf) {
  1254. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1255. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1256. xfer->tx_nbits != SPI_NBITS_QUAD)
  1257. return -EINVAL;
  1258. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1259. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1260. return -EINVAL;
  1261. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1262. !(spi->mode & SPI_TX_QUAD))
  1263. return -EINVAL;
  1264. if ((spi->mode & SPI_3WIRE) &&
  1265. (xfer->tx_nbits != SPI_NBITS_SINGLE))
  1266. return -EINVAL;
  1267. }
  1268. /* check transfer rx_nbits */
  1269. if (xfer->rx_buf) {
  1270. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1271. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1272. xfer->rx_nbits != SPI_NBITS_QUAD)
  1273. return -EINVAL;
  1274. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1275. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1276. return -EINVAL;
  1277. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1278. !(spi->mode & SPI_RX_QUAD))
  1279. return -EINVAL;
  1280. if ((spi->mode & SPI_3WIRE) &&
  1281. (xfer->rx_nbits != SPI_NBITS_SINGLE))
  1282. return -EINVAL;
  1283. }
  1284. }
  1285. message->spi = spi;
  1286. message->status = -EINPROGRESS;
  1287. return master->transfer(spi, message);
  1288. }
  1289. /**
  1290. * spi_async - asynchronous SPI transfer
  1291. * @spi: device with which data will be exchanged
  1292. * @message: describes the data transfers, including completion callback
  1293. * Context: any (irqs may be blocked, etc)
  1294. *
  1295. * This call may be used in_irq and other contexts which can't sleep,
  1296. * as well as from task contexts which can sleep.
  1297. *
  1298. * The completion callback is invoked in a context which can't sleep.
  1299. * Before that invocation, the value of message->status is undefined.
  1300. * When the callback is issued, message->status holds either zero (to
  1301. * indicate complete success) or a negative error code. After that
  1302. * callback returns, the driver which issued the transfer request may
  1303. * deallocate the associated memory; it's no longer in use by any SPI
  1304. * core or controller driver code.
  1305. *
  1306. * Note that although all messages to a spi_device are handled in
  1307. * FIFO order, messages may go to different devices in other orders.
  1308. * Some device might be higher priority, or have various "hard" access
  1309. * time requirements, for example.
  1310. *
  1311. * On detection of any fault during the transfer, processing of
  1312. * the entire message is aborted, and the device is deselected.
  1313. * Until returning from the associated message completion callback,
  1314. * no other spi_message queued to that device will be processed.
  1315. * (This rule applies equally to all the synchronous transfer calls,
  1316. * which are wrappers around this core asynchronous primitive.)
  1317. */
  1318. int spi_async(struct spi_device *spi, struct spi_message *message)
  1319. {
  1320. struct spi_master *master = spi->master;
  1321. int ret;
  1322. unsigned long flags;
  1323. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1324. if (master->bus_lock_flag)
  1325. ret = -EBUSY;
  1326. else
  1327. ret = __spi_async(spi, message);
  1328. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1329. return ret;
  1330. }
  1331. EXPORT_SYMBOL_GPL(spi_async);
  1332. /**
  1333. * spi_async_locked - version of spi_async with exclusive bus usage
  1334. * @spi: device with which data will be exchanged
  1335. * @message: describes the data transfers, including completion callback
  1336. * Context: any (irqs may be blocked, etc)
  1337. *
  1338. * This call may be used in_irq and other contexts which can't sleep,
  1339. * as well as from task contexts which can sleep.
  1340. *
  1341. * The completion callback is invoked in a context which can't sleep.
  1342. * Before that invocation, the value of message->status is undefined.
  1343. * When the callback is issued, message->status holds either zero (to
  1344. * indicate complete success) or a negative error code. After that
  1345. * callback returns, the driver which issued the transfer request may
  1346. * deallocate the associated memory; it's no longer in use by any SPI
  1347. * core or controller driver code.
  1348. *
  1349. * Note that although all messages to a spi_device are handled in
  1350. * FIFO order, messages may go to different devices in other orders.
  1351. * Some device might be higher priority, or have various "hard" access
  1352. * time requirements, for example.
  1353. *
  1354. * On detection of any fault during the transfer, processing of
  1355. * the entire message is aborted, and the device is deselected.
  1356. * Until returning from the associated message completion callback,
  1357. * no other spi_message queued to that device will be processed.
  1358. * (This rule applies equally to all the synchronous transfer calls,
  1359. * which are wrappers around this core asynchronous primitive.)
  1360. */
  1361. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1362. {
  1363. struct spi_master *master = spi->master;
  1364. int ret;
  1365. unsigned long flags;
  1366. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1367. ret = __spi_async(spi, message);
  1368. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1369. return ret;
  1370. }
  1371. EXPORT_SYMBOL_GPL(spi_async_locked);
  1372. /*-------------------------------------------------------------------------*/
  1373. /* Utility methods for SPI master protocol drivers, layered on
  1374. * top of the core. Some other utility methods are defined as
  1375. * inline functions.
  1376. */
  1377. static void spi_complete(void *arg)
  1378. {
  1379. complete(arg);
  1380. }
  1381. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1382. int bus_locked)
  1383. {
  1384. DECLARE_COMPLETION_ONSTACK(done);
  1385. int status;
  1386. struct spi_master *master = spi->master;
  1387. message->complete = spi_complete;
  1388. message->context = &done;
  1389. if (!bus_locked)
  1390. mutex_lock(&master->bus_lock_mutex);
  1391. status = spi_async_locked(spi, message);
  1392. if (!bus_locked)
  1393. mutex_unlock(&master->bus_lock_mutex);
  1394. if (status == 0) {
  1395. wait_for_completion(&done);
  1396. status = message->status;
  1397. }
  1398. message->context = NULL;
  1399. return status;
  1400. }
  1401. /**
  1402. * spi_sync - blocking/synchronous SPI data transfers
  1403. * @spi: device with which data will be exchanged
  1404. * @message: describes the data transfers
  1405. * Context: can sleep
  1406. *
  1407. * This call may only be used from a context that may sleep. The sleep
  1408. * is non-interruptible, and has no timeout. Low-overhead controller
  1409. * drivers may DMA directly into and out of the message buffers.
  1410. *
  1411. * Note that the SPI device's chip select is active during the message,
  1412. * and then is normally disabled between messages. Drivers for some
  1413. * frequently-used devices may want to minimize costs of selecting a chip,
  1414. * by leaving it selected in anticipation that the next message will go
  1415. * to the same chip. (That may increase power usage.)
  1416. *
  1417. * Also, the caller is guaranteeing that the memory associated with the
  1418. * message will not be freed before this call returns.
  1419. *
  1420. * It returns zero on success, else a negative error code.
  1421. */
  1422. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1423. {
  1424. return __spi_sync(spi, message, 0);
  1425. }
  1426. EXPORT_SYMBOL_GPL(spi_sync);
  1427. /**
  1428. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1429. * @spi: device with which data will be exchanged
  1430. * @message: describes the data transfers
  1431. * Context: can sleep
  1432. *
  1433. * This call may only be used from a context that may sleep. The sleep
  1434. * is non-interruptible, and has no timeout. Low-overhead controller
  1435. * drivers may DMA directly into and out of the message buffers.
  1436. *
  1437. * This call should be used by drivers that require exclusive access to the
  1438. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1439. * be released by a spi_bus_unlock call when the exclusive access is over.
  1440. *
  1441. * It returns zero on success, else a negative error code.
  1442. */
  1443. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1444. {
  1445. return __spi_sync(spi, message, 1);
  1446. }
  1447. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1448. /**
  1449. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1450. * @master: SPI bus master that should be locked for exclusive bus access
  1451. * Context: can sleep
  1452. *
  1453. * This call may only be used from a context that may sleep. The sleep
  1454. * is non-interruptible, and has no timeout.
  1455. *
  1456. * This call should be used by drivers that require exclusive access to the
  1457. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1458. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1459. * and spi_async_locked calls when the SPI bus lock is held.
  1460. *
  1461. * It returns zero on success, else a negative error code.
  1462. */
  1463. int spi_bus_lock(struct spi_master *master)
  1464. {
  1465. unsigned long flags;
  1466. mutex_lock(&master->bus_lock_mutex);
  1467. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1468. master->bus_lock_flag = 1;
  1469. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1470. /* mutex remains locked until spi_bus_unlock is called */
  1471. return 0;
  1472. }
  1473. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1474. /**
  1475. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1476. * @master: SPI bus master that was locked for exclusive bus access
  1477. * Context: can sleep
  1478. *
  1479. * This call may only be used from a context that may sleep. The sleep
  1480. * is non-interruptible, and has no timeout.
  1481. *
  1482. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1483. * call.
  1484. *
  1485. * It returns zero on success, else a negative error code.
  1486. */
  1487. int spi_bus_unlock(struct spi_master *master)
  1488. {
  1489. master->bus_lock_flag = 0;
  1490. mutex_unlock(&master->bus_lock_mutex);
  1491. return 0;
  1492. }
  1493. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1494. /* portable code must never pass more than 32 bytes */
  1495. #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
  1496. static u8 *buf;
  1497. /**
  1498. * spi_write_then_read - SPI synchronous write followed by read
  1499. * @spi: device with which data will be exchanged
  1500. * @txbuf: data to be written (need not be dma-safe)
  1501. * @n_tx: size of txbuf, in bytes
  1502. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1503. * @n_rx: size of rxbuf, in bytes
  1504. * Context: can sleep
  1505. *
  1506. * This performs a half duplex MicroWire style transaction with the
  1507. * device, sending txbuf and then reading rxbuf. The return value
  1508. * is zero for success, else a negative errno status code.
  1509. * This call may only be used from a context that may sleep.
  1510. *
  1511. * Parameters to this routine are always copied using a small buffer;
  1512. * portable code should never use this for more than 32 bytes.
  1513. * Performance-sensitive or bulk transfer code should instead use
  1514. * spi_{async,sync}() calls with dma-safe buffers.
  1515. */
  1516. int spi_write_then_read(struct spi_device *spi,
  1517. const void *txbuf, unsigned n_tx,
  1518. void *rxbuf, unsigned n_rx)
  1519. {
  1520. static DEFINE_MUTEX(lock);
  1521. int status;
  1522. struct spi_message message;
  1523. struct spi_transfer x[2];
  1524. u8 *local_buf;
  1525. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1526. * copying here, (as a pure convenience thing), but we can
  1527. * keep heap costs out of the hot path unless someone else is
  1528. * using the pre-allocated buffer or the transfer is too large.
  1529. */
  1530. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1531. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1532. GFP_KERNEL | GFP_DMA);
  1533. if (!local_buf)
  1534. return -ENOMEM;
  1535. } else {
  1536. local_buf = buf;
  1537. }
  1538. spi_message_init(&message);
  1539. memset(x, 0, sizeof x);
  1540. if (n_tx) {
  1541. x[0].len = n_tx;
  1542. spi_message_add_tail(&x[0], &message);
  1543. }
  1544. if (n_rx) {
  1545. x[1].len = n_rx;
  1546. spi_message_add_tail(&x[1], &message);
  1547. }
  1548. memcpy(local_buf, txbuf, n_tx);
  1549. x[0].tx_buf = local_buf;
  1550. x[1].rx_buf = local_buf + n_tx;
  1551. /* do the i/o */
  1552. status = spi_sync(spi, &message);
  1553. if (status == 0)
  1554. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1555. if (x[0].tx_buf == buf)
  1556. mutex_unlock(&lock);
  1557. else
  1558. kfree(local_buf);
  1559. return status;
  1560. }
  1561. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1562. /*-------------------------------------------------------------------------*/
  1563. static int __init spi_init(void)
  1564. {
  1565. int status;
  1566. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1567. if (!buf) {
  1568. status = -ENOMEM;
  1569. goto err0;
  1570. }
  1571. status = bus_register(&spi_bus_type);
  1572. if (status < 0)
  1573. goto err1;
  1574. status = class_register(&spi_master_class);
  1575. if (status < 0)
  1576. goto err2;
  1577. return 0;
  1578. err2:
  1579. bus_unregister(&spi_bus_type);
  1580. err1:
  1581. kfree(buf);
  1582. buf = NULL;
  1583. err0:
  1584. return status;
  1585. }
  1586. /* board_info is normally registered in arch_initcall(),
  1587. * but even essential drivers wait till later
  1588. *
  1589. * REVISIT only boardinfo really needs static linking. the rest (device and
  1590. * driver registration) _could_ be dynamically linked (modular) ... costs
  1591. * include needing to have boardinfo data structures be much more public.
  1592. */
  1593. postcore_initcall(spi_init);