spi.c 55 KB

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