spi.c 55 KB

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