spi.c 56 KB

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