spi.c 55 KB

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