spi.c 59 KB

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