spi.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  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. int ret;
  220. acpi_dev_pm_attach(dev, true);
  221. ret = sdrv->probe(to_spi_device(dev));
  222. if (ret)
  223. acpi_dev_pm_detach(dev, true);
  224. return ret;
  225. }
  226. static int spi_drv_remove(struct device *dev)
  227. {
  228. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  229. int ret;
  230. ret = sdrv->remove(to_spi_device(dev));
  231. acpi_dev_pm_detach(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. static int spi_map_buf(struct spi_master *master, struct device *dev,
  493. struct sg_table *sgt, void *buf, size_t len,
  494. enum dma_data_direction dir)
  495. {
  496. const bool vmalloced_buf = is_vmalloc_addr(buf);
  497. const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len;
  498. const int sgs = DIV_ROUND_UP(len, desc_len);
  499. struct page *vm_page;
  500. void *sg_buf;
  501. size_t min;
  502. int i, ret;
  503. ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
  504. if (ret != 0)
  505. return ret;
  506. for (i = 0; i < sgs; i++) {
  507. min = min_t(size_t, len, desc_len);
  508. if (vmalloced_buf) {
  509. vm_page = vmalloc_to_page(buf);
  510. if (!vm_page) {
  511. sg_free_table(sgt);
  512. return -ENOMEM;
  513. }
  514. sg_buf = page_address(vm_page) +
  515. ((size_t)buf & ~PAGE_MASK);
  516. } else {
  517. sg_buf = buf;
  518. }
  519. sg_set_buf(&sgt->sgl[i], sg_buf, min);
  520. buf += min;
  521. len -= min;
  522. }
  523. ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
  524. if (ret < 0) {
  525. sg_free_table(sgt);
  526. return ret;
  527. }
  528. sgt->nents = ret;
  529. return 0;
  530. }
  531. static void spi_unmap_buf(struct spi_master *master, struct device *dev,
  532. struct sg_table *sgt, enum dma_data_direction dir)
  533. {
  534. if (sgt->orig_nents) {
  535. dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  536. sg_free_table(sgt);
  537. }
  538. }
  539. static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
  540. {
  541. struct device *tx_dev, *rx_dev;
  542. struct spi_transfer *xfer;
  543. void *tmp;
  544. unsigned int max_tx, max_rx;
  545. int ret;
  546. if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
  547. max_tx = 0;
  548. max_rx = 0;
  549. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  550. if ((master->flags & SPI_MASTER_MUST_TX) &&
  551. !xfer->tx_buf)
  552. max_tx = max(xfer->len, max_tx);
  553. if ((master->flags & SPI_MASTER_MUST_RX) &&
  554. !xfer->rx_buf)
  555. max_rx = max(xfer->len, max_rx);
  556. }
  557. if (max_tx) {
  558. tmp = krealloc(master->dummy_tx, max_tx,
  559. GFP_KERNEL | GFP_DMA);
  560. if (!tmp)
  561. return -ENOMEM;
  562. master->dummy_tx = tmp;
  563. memset(tmp, 0, max_tx);
  564. }
  565. if (max_rx) {
  566. tmp = krealloc(master->dummy_rx, max_rx,
  567. GFP_KERNEL | GFP_DMA);
  568. if (!tmp)
  569. return -ENOMEM;
  570. master->dummy_rx = tmp;
  571. }
  572. if (max_tx || max_rx) {
  573. list_for_each_entry(xfer, &msg->transfers,
  574. transfer_list) {
  575. if (!xfer->tx_buf)
  576. xfer->tx_buf = master->dummy_tx;
  577. if (!xfer->rx_buf)
  578. xfer->rx_buf = master->dummy_rx;
  579. }
  580. }
  581. }
  582. if (!master->can_dma)
  583. return 0;
  584. tx_dev = &master->dma_tx->dev->device;
  585. rx_dev = &master->dma_rx->dev->device;
  586. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  587. if (!master->can_dma(master, msg->spi, xfer))
  588. continue;
  589. if (xfer->tx_buf != NULL) {
  590. ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
  591. (void *)xfer->tx_buf, xfer->len,
  592. DMA_TO_DEVICE);
  593. if (ret != 0)
  594. return ret;
  595. }
  596. if (xfer->rx_buf != NULL) {
  597. ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
  598. xfer->rx_buf, xfer->len,
  599. DMA_FROM_DEVICE);
  600. if (ret != 0) {
  601. spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
  602. DMA_TO_DEVICE);
  603. return ret;
  604. }
  605. }
  606. }
  607. master->cur_msg_mapped = true;
  608. return 0;
  609. }
  610. static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
  611. {
  612. struct spi_transfer *xfer;
  613. struct device *tx_dev, *rx_dev;
  614. if (!master->cur_msg_mapped || !master->can_dma)
  615. return 0;
  616. tx_dev = &master->dma_tx->dev->device;
  617. rx_dev = &master->dma_rx->dev->device;
  618. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  619. if (!master->can_dma(master, msg->spi, xfer))
  620. continue;
  621. spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
  622. spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
  623. }
  624. return 0;
  625. }
  626. /*
  627. * spi_transfer_one_message - Default implementation of transfer_one_message()
  628. *
  629. * This is a standard implementation of transfer_one_message() for
  630. * drivers which impelment a transfer_one() operation. It provides
  631. * standard handling of delays and chip select management.
  632. */
  633. static int spi_transfer_one_message(struct spi_master *master,
  634. struct spi_message *msg)
  635. {
  636. struct spi_transfer *xfer;
  637. bool keep_cs = false;
  638. int ret = 0;
  639. int ms = 1;
  640. spi_set_cs(msg->spi, true);
  641. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  642. trace_spi_transfer_start(msg, xfer);
  643. reinit_completion(&master->xfer_completion);
  644. ret = master->transfer_one(master, msg->spi, xfer);
  645. if (ret < 0) {
  646. dev_err(&msg->spi->dev,
  647. "SPI transfer failed: %d\n", ret);
  648. goto out;
  649. }
  650. if (ret > 0) {
  651. ret = 0;
  652. ms = xfer->len * 8 * 1000 / xfer->speed_hz;
  653. ms += 10; /* some tolerance */
  654. ms = wait_for_completion_timeout(&master->xfer_completion,
  655. msecs_to_jiffies(ms));
  656. }
  657. if (ms == 0) {
  658. dev_err(&msg->spi->dev, "SPI transfer timed out\n");
  659. msg->status = -ETIMEDOUT;
  660. }
  661. trace_spi_transfer_stop(msg, xfer);
  662. if (msg->status != -EINPROGRESS)
  663. goto out;
  664. if (xfer->delay_usecs)
  665. udelay(xfer->delay_usecs);
  666. if (xfer->cs_change) {
  667. if (list_is_last(&xfer->transfer_list,
  668. &msg->transfers)) {
  669. keep_cs = true;
  670. } else {
  671. spi_set_cs(msg->spi, false);
  672. udelay(10);
  673. spi_set_cs(msg->spi, true);
  674. }
  675. }
  676. msg->actual_length += xfer->len;
  677. }
  678. out:
  679. if (ret != 0 || !keep_cs)
  680. spi_set_cs(msg->spi, false);
  681. if (msg->status == -EINPROGRESS)
  682. msg->status = ret;
  683. spi_finalize_current_message(master);
  684. return ret;
  685. }
  686. /**
  687. * spi_finalize_current_transfer - report completion of a transfer
  688. *
  689. * Called by SPI drivers using the core transfer_one_message()
  690. * implementation to notify it that the current interrupt driven
  691. * transfer has finished and the next one may be scheduled.
  692. */
  693. void spi_finalize_current_transfer(struct spi_master *master)
  694. {
  695. complete(&master->xfer_completion);
  696. }
  697. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  698. /**
  699. * spi_pump_messages - kthread work function which processes spi message queue
  700. * @work: pointer to kthread work struct contained in the master struct
  701. *
  702. * This function checks if there is any spi message in the queue that
  703. * needs processing and if so call out to the driver to initialize hardware
  704. * and transfer each message.
  705. *
  706. */
  707. static void spi_pump_messages(struct kthread_work *work)
  708. {
  709. struct spi_master *master =
  710. container_of(work, struct spi_master, pump_messages);
  711. unsigned long flags;
  712. bool was_busy = false;
  713. int ret;
  714. /* Lock queue and check for queue work */
  715. spin_lock_irqsave(&master->queue_lock, flags);
  716. if (list_empty(&master->queue) || !master->running) {
  717. if (!master->busy) {
  718. spin_unlock_irqrestore(&master->queue_lock, flags);
  719. return;
  720. }
  721. master->busy = false;
  722. spin_unlock_irqrestore(&master->queue_lock, flags);
  723. kfree(master->dummy_rx);
  724. master->dummy_rx = NULL;
  725. kfree(master->dummy_tx);
  726. master->dummy_tx = NULL;
  727. if (master->unprepare_transfer_hardware &&
  728. master->unprepare_transfer_hardware(master))
  729. dev_err(&master->dev,
  730. "failed to unprepare transfer hardware\n");
  731. if (master->auto_runtime_pm) {
  732. pm_runtime_mark_last_busy(master->dev.parent);
  733. pm_runtime_put_autosuspend(master->dev.parent);
  734. }
  735. trace_spi_master_idle(master);
  736. return;
  737. }
  738. /* Make sure we are not already running a message */
  739. if (master->cur_msg) {
  740. spin_unlock_irqrestore(&master->queue_lock, flags);
  741. return;
  742. }
  743. /* Extract head of queue */
  744. master->cur_msg =
  745. list_first_entry(&master->queue, struct spi_message, queue);
  746. list_del_init(&master->cur_msg->queue);
  747. if (master->busy)
  748. was_busy = true;
  749. else
  750. master->busy = true;
  751. spin_unlock_irqrestore(&master->queue_lock, flags);
  752. if (!was_busy && master->auto_runtime_pm) {
  753. ret = pm_runtime_get_sync(master->dev.parent);
  754. if (ret < 0) {
  755. dev_err(&master->dev, "Failed to power device: %d\n",
  756. ret);
  757. return;
  758. }
  759. }
  760. if (!was_busy)
  761. trace_spi_master_busy(master);
  762. if (!was_busy && master->prepare_transfer_hardware) {
  763. ret = master->prepare_transfer_hardware(master);
  764. if (ret) {
  765. dev_err(&master->dev,
  766. "failed to prepare transfer hardware\n");
  767. if (master->auto_runtime_pm)
  768. pm_runtime_put(master->dev.parent);
  769. return;
  770. }
  771. }
  772. trace_spi_message_start(master->cur_msg);
  773. if (master->prepare_message) {
  774. ret = master->prepare_message(master, master->cur_msg);
  775. if (ret) {
  776. dev_err(&master->dev,
  777. "failed to prepare message: %d\n", ret);
  778. master->cur_msg->status = ret;
  779. spi_finalize_current_message(master);
  780. return;
  781. }
  782. master->cur_msg_prepared = true;
  783. }
  784. ret = spi_map_msg(master, master->cur_msg);
  785. if (ret) {
  786. master->cur_msg->status = ret;
  787. spi_finalize_current_message(master);
  788. return;
  789. }
  790. ret = master->transfer_one_message(master, master->cur_msg);
  791. if (ret) {
  792. dev_err(&master->dev,
  793. "failed to transfer one message from queue\n");
  794. return;
  795. }
  796. }
  797. static int spi_init_queue(struct spi_master *master)
  798. {
  799. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  800. INIT_LIST_HEAD(&master->queue);
  801. spin_lock_init(&master->queue_lock);
  802. master->running = false;
  803. master->busy = false;
  804. init_kthread_worker(&master->kworker);
  805. master->kworker_task = kthread_run(kthread_worker_fn,
  806. &master->kworker, "%s",
  807. dev_name(&master->dev));
  808. if (IS_ERR(master->kworker_task)) {
  809. dev_err(&master->dev, "failed to create message pump task\n");
  810. return -ENOMEM;
  811. }
  812. init_kthread_work(&master->pump_messages, spi_pump_messages);
  813. /*
  814. * Master config will indicate if this controller should run the
  815. * message pump with high (realtime) priority to reduce the transfer
  816. * latency on the bus by minimising the delay between a transfer
  817. * request and the scheduling of the message pump thread. Without this
  818. * setting the message pump thread will remain at default priority.
  819. */
  820. if (master->rt) {
  821. dev_info(&master->dev,
  822. "will run message pump with realtime priority\n");
  823. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  824. }
  825. return 0;
  826. }
  827. /**
  828. * spi_get_next_queued_message() - called by driver to check for queued
  829. * messages
  830. * @master: the master to check for queued messages
  831. *
  832. * If there are more messages in the queue, the next message is returned from
  833. * this call.
  834. */
  835. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  836. {
  837. struct spi_message *next;
  838. unsigned long flags;
  839. /* get a pointer to the next message, if any */
  840. spin_lock_irqsave(&master->queue_lock, flags);
  841. next = list_first_entry_or_null(&master->queue, struct spi_message,
  842. queue);
  843. spin_unlock_irqrestore(&master->queue_lock, flags);
  844. return next;
  845. }
  846. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  847. /**
  848. * spi_finalize_current_message() - the current message is complete
  849. * @master: the master to return the message to
  850. *
  851. * Called by the driver to notify the core that the message in the front of the
  852. * queue is complete and can be removed from the queue.
  853. */
  854. void spi_finalize_current_message(struct spi_master *master)
  855. {
  856. struct spi_message *mesg;
  857. unsigned long flags;
  858. int ret;
  859. spin_lock_irqsave(&master->queue_lock, flags);
  860. mesg = master->cur_msg;
  861. master->cur_msg = NULL;
  862. queue_kthread_work(&master->kworker, &master->pump_messages);
  863. spin_unlock_irqrestore(&master->queue_lock, flags);
  864. spi_unmap_msg(master, mesg);
  865. if (master->cur_msg_prepared && master->unprepare_message) {
  866. ret = master->unprepare_message(master, mesg);
  867. if (ret) {
  868. dev_err(&master->dev,
  869. "failed to unprepare message: %d\n", ret);
  870. }
  871. }
  872. master->cur_msg_prepared = false;
  873. mesg->state = NULL;
  874. if (mesg->complete)
  875. mesg->complete(mesg->context);
  876. trace_spi_message_done(mesg);
  877. }
  878. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  879. static int spi_start_queue(struct spi_master *master)
  880. {
  881. unsigned long flags;
  882. spin_lock_irqsave(&master->queue_lock, flags);
  883. if (master->running || master->busy) {
  884. spin_unlock_irqrestore(&master->queue_lock, flags);
  885. return -EBUSY;
  886. }
  887. master->running = true;
  888. master->cur_msg = NULL;
  889. spin_unlock_irqrestore(&master->queue_lock, flags);
  890. queue_kthread_work(&master->kworker, &master->pump_messages);
  891. return 0;
  892. }
  893. static int spi_stop_queue(struct spi_master *master)
  894. {
  895. unsigned long flags;
  896. unsigned limit = 500;
  897. int ret = 0;
  898. spin_lock_irqsave(&master->queue_lock, flags);
  899. /*
  900. * This is a bit lame, but is optimized for the common execution path.
  901. * A wait_queue on the master->busy could be used, but then the common
  902. * execution path (pump_messages) would be required to call wake_up or
  903. * friends on every SPI message. Do this instead.
  904. */
  905. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  906. spin_unlock_irqrestore(&master->queue_lock, flags);
  907. usleep_range(10000, 11000);
  908. spin_lock_irqsave(&master->queue_lock, flags);
  909. }
  910. if (!list_empty(&master->queue) || master->busy)
  911. ret = -EBUSY;
  912. else
  913. master->running = false;
  914. spin_unlock_irqrestore(&master->queue_lock, flags);
  915. if (ret) {
  916. dev_warn(&master->dev,
  917. "could not stop message queue\n");
  918. return ret;
  919. }
  920. return ret;
  921. }
  922. static int spi_destroy_queue(struct spi_master *master)
  923. {
  924. int ret;
  925. ret = spi_stop_queue(master);
  926. /*
  927. * flush_kthread_worker will block until all work is done.
  928. * If the reason that stop_queue timed out is that the work will never
  929. * finish, then it does no good to call flush/stop thread, so
  930. * return anyway.
  931. */
  932. if (ret) {
  933. dev_err(&master->dev, "problem destroying queue\n");
  934. return ret;
  935. }
  936. flush_kthread_worker(&master->kworker);
  937. kthread_stop(master->kworker_task);
  938. return 0;
  939. }
  940. /**
  941. * spi_queued_transfer - transfer function for queued transfers
  942. * @spi: spi device which is requesting transfer
  943. * @msg: spi message which is to handled is queued to driver queue
  944. */
  945. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  946. {
  947. struct spi_master *master = spi->master;
  948. unsigned long flags;
  949. spin_lock_irqsave(&master->queue_lock, flags);
  950. if (!master->running) {
  951. spin_unlock_irqrestore(&master->queue_lock, flags);
  952. return -ESHUTDOWN;
  953. }
  954. msg->actual_length = 0;
  955. msg->status = -EINPROGRESS;
  956. list_add_tail(&msg->queue, &master->queue);
  957. if (!master->busy)
  958. queue_kthread_work(&master->kworker, &master->pump_messages);
  959. spin_unlock_irqrestore(&master->queue_lock, flags);
  960. return 0;
  961. }
  962. static int spi_master_initialize_queue(struct spi_master *master)
  963. {
  964. int ret;
  965. master->queued = true;
  966. master->transfer = spi_queued_transfer;
  967. if (!master->transfer_one_message)
  968. master->transfer_one_message = spi_transfer_one_message;
  969. /* Initialize and start queue */
  970. ret = spi_init_queue(master);
  971. if (ret) {
  972. dev_err(&master->dev, "problem initializing queue\n");
  973. goto err_init_queue;
  974. }
  975. ret = spi_start_queue(master);
  976. if (ret) {
  977. dev_err(&master->dev, "problem starting queue\n");
  978. goto err_start_queue;
  979. }
  980. return 0;
  981. err_start_queue:
  982. err_init_queue:
  983. spi_destroy_queue(master);
  984. return ret;
  985. }
  986. /*-------------------------------------------------------------------------*/
  987. #if defined(CONFIG_OF)
  988. /**
  989. * of_register_spi_devices() - Register child devices onto the SPI bus
  990. * @master: Pointer to spi_master device
  991. *
  992. * Registers an spi_device for each child node of master node which has a 'reg'
  993. * property.
  994. */
  995. static void of_register_spi_devices(struct spi_master *master)
  996. {
  997. struct spi_device *spi;
  998. struct device_node *nc;
  999. int rc;
  1000. u32 value;
  1001. if (!master->dev.of_node)
  1002. return;
  1003. for_each_available_child_of_node(master->dev.of_node, nc) {
  1004. /* Alloc an spi_device */
  1005. spi = spi_alloc_device(master);
  1006. if (!spi) {
  1007. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1008. nc->full_name);
  1009. spi_dev_put(spi);
  1010. continue;
  1011. }
  1012. /* Select device driver */
  1013. if (of_modalias_node(nc, spi->modalias,
  1014. sizeof(spi->modalias)) < 0) {
  1015. dev_err(&master->dev, "cannot find modalias for %s\n",
  1016. nc->full_name);
  1017. spi_dev_put(spi);
  1018. continue;
  1019. }
  1020. /* Device address */
  1021. rc = of_property_read_u32(nc, "reg", &value);
  1022. if (rc) {
  1023. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1024. nc->full_name, rc);
  1025. spi_dev_put(spi);
  1026. continue;
  1027. }
  1028. spi->chip_select = value;
  1029. /* Mode (clock phase/polarity/etc.) */
  1030. if (of_find_property(nc, "spi-cpha", NULL))
  1031. spi->mode |= SPI_CPHA;
  1032. if (of_find_property(nc, "spi-cpol", NULL))
  1033. spi->mode |= SPI_CPOL;
  1034. if (of_find_property(nc, "spi-cs-high", NULL))
  1035. spi->mode |= SPI_CS_HIGH;
  1036. if (of_find_property(nc, "spi-3wire", NULL))
  1037. spi->mode |= SPI_3WIRE;
  1038. /* Device DUAL/QUAD mode */
  1039. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1040. switch (value) {
  1041. case 1:
  1042. break;
  1043. case 2:
  1044. spi->mode |= SPI_TX_DUAL;
  1045. break;
  1046. case 4:
  1047. spi->mode |= SPI_TX_QUAD;
  1048. break;
  1049. default:
  1050. dev_err(&master->dev,
  1051. "spi-tx-bus-width %d not supported\n",
  1052. value);
  1053. spi_dev_put(spi);
  1054. continue;
  1055. }
  1056. }
  1057. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1058. switch (value) {
  1059. case 1:
  1060. break;
  1061. case 2:
  1062. spi->mode |= SPI_RX_DUAL;
  1063. break;
  1064. case 4:
  1065. spi->mode |= SPI_RX_QUAD;
  1066. break;
  1067. default:
  1068. dev_err(&master->dev,
  1069. "spi-rx-bus-width %d not supported\n",
  1070. value);
  1071. spi_dev_put(spi);
  1072. continue;
  1073. }
  1074. }
  1075. /* Device speed */
  1076. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1077. if (rc) {
  1078. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1079. nc->full_name, rc);
  1080. spi_dev_put(spi);
  1081. continue;
  1082. }
  1083. spi->max_speed_hz = value;
  1084. /* IRQ */
  1085. spi->irq = irq_of_parse_and_map(nc, 0);
  1086. /* Store a pointer to the node in the device structure */
  1087. of_node_get(nc);
  1088. spi->dev.of_node = nc;
  1089. /* Register the new device */
  1090. request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
  1091. rc = spi_add_device(spi);
  1092. if (rc) {
  1093. dev_err(&master->dev, "spi_device register error %s\n",
  1094. nc->full_name);
  1095. spi_dev_put(spi);
  1096. }
  1097. }
  1098. }
  1099. #else
  1100. static void of_register_spi_devices(struct spi_master *master) { }
  1101. #endif
  1102. #ifdef CONFIG_ACPI
  1103. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1104. {
  1105. struct spi_device *spi = data;
  1106. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1107. struct acpi_resource_spi_serialbus *sb;
  1108. sb = &ares->data.spi_serial_bus;
  1109. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1110. spi->chip_select = sb->device_selection;
  1111. spi->max_speed_hz = sb->connection_speed;
  1112. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1113. spi->mode |= SPI_CPHA;
  1114. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1115. spi->mode |= SPI_CPOL;
  1116. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1117. spi->mode |= SPI_CS_HIGH;
  1118. }
  1119. } else if (spi->irq < 0) {
  1120. struct resource r;
  1121. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1122. spi->irq = r.start;
  1123. }
  1124. /* Always tell the ACPI core to skip this resource */
  1125. return 1;
  1126. }
  1127. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1128. void *data, void **return_value)
  1129. {
  1130. struct spi_master *master = data;
  1131. struct list_head resource_list;
  1132. struct acpi_device *adev;
  1133. struct spi_device *spi;
  1134. int ret;
  1135. if (acpi_bus_get_device(handle, &adev))
  1136. return AE_OK;
  1137. if (acpi_bus_get_status(adev) || !adev->status.present)
  1138. return AE_OK;
  1139. spi = spi_alloc_device(master);
  1140. if (!spi) {
  1141. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1142. dev_name(&adev->dev));
  1143. return AE_NO_MEMORY;
  1144. }
  1145. ACPI_COMPANION_SET(&spi->dev, adev);
  1146. spi->irq = -1;
  1147. INIT_LIST_HEAD(&resource_list);
  1148. ret = acpi_dev_get_resources(adev, &resource_list,
  1149. acpi_spi_add_resource, spi);
  1150. acpi_dev_free_resource_list(&resource_list);
  1151. if (ret < 0 || !spi->max_speed_hz) {
  1152. spi_dev_put(spi);
  1153. return AE_OK;
  1154. }
  1155. adev->power.flags.ignore_parent = true;
  1156. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1157. if (spi_add_device(spi)) {
  1158. adev->power.flags.ignore_parent = false;
  1159. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1160. dev_name(&adev->dev));
  1161. spi_dev_put(spi);
  1162. }
  1163. return AE_OK;
  1164. }
  1165. static void acpi_register_spi_devices(struct spi_master *master)
  1166. {
  1167. acpi_status status;
  1168. acpi_handle handle;
  1169. handle = ACPI_HANDLE(master->dev.parent);
  1170. if (!handle)
  1171. return;
  1172. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1173. acpi_spi_add_device, NULL,
  1174. master, NULL);
  1175. if (ACPI_FAILURE(status))
  1176. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1177. }
  1178. #else
  1179. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1180. #endif /* CONFIG_ACPI */
  1181. static void spi_master_release(struct device *dev)
  1182. {
  1183. struct spi_master *master;
  1184. master = container_of(dev, struct spi_master, dev);
  1185. kfree(master);
  1186. }
  1187. static struct class spi_master_class = {
  1188. .name = "spi_master",
  1189. .owner = THIS_MODULE,
  1190. .dev_release = spi_master_release,
  1191. };
  1192. /**
  1193. * spi_alloc_master - allocate SPI master controller
  1194. * @dev: the controller, possibly using the platform_bus
  1195. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1196. * memory is in the driver_data field of the returned device,
  1197. * accessible with spi_master_get_devdata().
  1198. * Context: can sleep
  1199. *
  1200. * This call is used only by SPI master controller drivers, which are the
  1201. * only ones directly touching chip registers. It's how they allocate
  1202. * an spi_master structure, prior to calling spi_register_master().
  1203. *
  1204. * This must be called from context that can sleep. It returns the SPI
  1205. * master structure on success, else NULL.
  1206. *
  1207. * The caller is responsible for assigning the bus number and initializing
  1208. * the master's methods before calling spi_register_master(); and (after errors
  1209. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1210. * leak.
  1211. */
  1212. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1213. {
  1214. struct spi_master *master;
  1215. if (!dev)
  1216. return NULL;
  1217. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1218. if (!master)
  1219. return NULL;
  1220. device_initialize(&master->dev);
  1221. master->bus_num = -1;
  1222. master->num_chipselect = 1;
  1223. master->dev.class = &spi_master_class;
  1224. master->dev.parent = get_device(dev);
  1225. spi_master_set_devdata(master, &master[1]);
  1226. return master;
  1227. }
  1228. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1229. #ifdef CONFIG_OF
  1230. static int of_spi_register_master(struct spi_master *master)
  1231. {
  1232. int nb, i, *cs;
  1233. struct device_node *np = master->dev.of_node;
  1234. if (!np)
  1235. return 0;
  1236. nb = of_gpio_named_count(np, "cs-gpios");
  1237. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1238. /* Return error only for an incorrectly formed cs-gpios property */
  1239. if (nb == 0 || nb == -ENOENT)
  1240. return 0;
  1241. else if (nb < 0)
  1242. return nb;
  1243. cs = devm_kzalloc(&master->dev,
  1244. sizeof(int) * master->num_chipselect,
  1245. GFP_KERNEL);
  1246. master->cs_gpios = cs;
  1247. if (!master->cs_gpios)
  1248. return -ENOMEM;
  1249. for (i = 0; i < master->num_chipselect; i++)
  1250. cs[i] = -ENOENT;
  1251. for (i = 0; i < nb; i++)
  1252. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1253. return 0;
  1254. }
  1255. #else
  1256. static int of_spi_register_master(struct spi_master *master)
  1257. {
  1258. return 0;
  1259. }
  1260. #endif
  1261. /**
  1262. * spi_register_master - register SPI master controller
  1263. * @master: initialized master, originally from spi_alloc_master()
  1264. * Context: can sleep
  1265. *
  1266. * SPI master controllers connect to their drivers using some non-SPI bus,
  1267. * such as the platform bus. The final stage of probe() in that code
  1268. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1269. *
  1270. * SPI controllers use board specific (often SOC specific) bus numbers,
  1271. * and board-specific addressing for SPI devices combines those numbers
  1272. * with chip select numbers. Since SPI does not directly support dynamic
  1273. * device identification, boards need configuration tables telling which
  1274. * chip is at which address.
  1275. *
  1276. * This must be called from context that can sleep. It returns zero on
  1277. * success, else a negative error code (dropping the master's refcount).
  1278. * After a successful return, the caller is responsible for calling
  1279. * spi_unregister_master().
  1280. */
  1281. int spi_register_master(struct spi_master *master)
  1282. {
  1283. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1284. struct device *dev = master->dev.parent;
  1285. struct boardinfo *bi;
  1286. int status = -ENODEV;
  1287. int dynamic = 0;
  1288. if (!dev)
  1289. return -ENODEV;
  1290. status = of_spi_register_master(master);
  1291. if (status)
  1292. return status;
  1293. /* even if it's just one always-selected device, there must
  1294. * be at least one chipselect
  1295. */
  1296. if (master->num_chipselect == 0)
  1297. return -EINVAL;
  1298. if ((master->bus_num < 0) && master->dev.of_node)
  1299. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1300. /* convention: dynamically assigned bus IDs count down from the max */
  1301. if (master->bus_num < 0) {
  1302. /* FIXME switch to an IDR based scheme, something like
  1303. * I2C now uses, so we can't run out of "dynamic" IDs
  1304. */
  1305. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1306. dynamic = 1;
  1307. }
  1308. spin_lock_init(&master->bus_lock_spinlock);
  1309. mutex_init(&master->bus_lock_mutex);
  1310. master->bus_lock_flag = 0;
  1311. init_completion(&master->xfer_completion);
  1312. if (!master->max_dma_len)
  1313. master->max_dma_len = INT_MAX;
  1314. /* register the device, then userspace will see it.
  1315. * registration fails if the bus ID is in use.
  1316. */
  1317. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1318. status = device_add(&master->dev);
  1319. if (status < 0)
  1320. goto done;
  1321. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1322. dynamic ? " (dynamic)" : "");
  1323. /* If we're using a queued driver, start the queue */
  1324. if (master->transfer)
  1325. dev_info(dev, "master is unqueued, this is deprecated\n");
  1326. else {
  1327. status = spi_master_initialize_queue(master);
  1328. if (status) {
  1329. device_del(&master->dev);
  1330. goto done;
  1331. }
  1332. }
  1333. mutex_lock(&board_lock);
  1334. list_add_tail(&master->list, &spi_master_list);
  1335. list_for_each_entry(bi, &board_list, list)
  1336. spi_match_master_to_boardinfo(master, &bi->board_info);
  1337. mutex_unlock(&board_lock);
  1338. /* Register devices from the device tree and ACPI */
  1339. of_register_spi_devices(master);
  1340. acpi_register_spi_devices(master);
  1341. done:
  1342. return status;
  1343. }
  1344. EXPORT_SYMBOL_GPL(spi_register_master);
  1345. static void devm_spi_unregister(struct device *dev, void *res)
  1346. {
  1347. spi_unregister_master(*(struct spi_master **)res);
  1348. }
  1349. /**
  1350. * dev_spi_register_master - register managed SPI master controller
  1351. * @dev: device managing SPI master
  1352. * @master: initialized master, originally from spi_alloc_master()
  1353. * Context: can sleep
  1354. *
  1355. * Register a SPI device as with spi_register_master() which will
  1356. * automatically be unregister
  1357. */
  1358. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1359. {
  1360. struct spi_master **ptr;
  1361. int ret;
  1362. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1363. if (!ptr)
  1364. return -ENOMEM;
  1365. ret = spi_register_master(master);
  1366. if (!ret) {
  1367. *ptr = master;
  1368. devres_add(dev, ptr);
  1369. } else {
  1370. devres_free(ptr);
  1371. }
  1372. return ret;
  1373. }
  1374. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1375. static int __unregister(struct device *dev, void *null)
  1376. {
  1377. spi_unregister_device(to_spi_device(dev));
  1378. return 0;
  1379. }
  1380. /**
  1381. * spi_unregister_master - unregister SPI master controller
  1382. * @master: the master being unregistered
  1383. * Context: can sleep
  1384. *
  1385. * This call is used only by SPI master controller drivers, which are the
  1386. * only ones directly touching chip registers.
  1387. *
  1388. * This must be called from context that can sleep.
  1389. */
  1390. void spi_unregister_master(struct spi_master *master)
  1391. {
  1392. int dummy;
  1393. if (master->queued) {
  1394. if (spi_destroy_queue(master))
  1395. dev_err(&master->dev, "queue remove failed\n");
  1396. }
  1397. mutex_lock(&board_lock);
  1398. list_del(&master->list);
  1399. mutex_unlock(&board_lock);
  1400. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1401. device_unregister(&master->dev);
  1402. }
  1403. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1404. int spi_master_suspend(struct spi_master *master)
  1405. {
  1406. int ret;
  1407. /* Basically no-ops for non-queued masters */
  1408. if (!master->queued)
  1409. return 0;
  1410. ret = spi_stop_queue(master);
  1411. if (ret)
  1412. dev_err(&master->dev, "queue stop failed\n");
  1413. return ret;
  1414. }
  1415. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1416. int spi_master_resume(struct spi_master *master)
  1417. {
  1418. int ret;
  1419. if (!master->queued)
  1420. return 0;
  1421. ret = spi_start_queue(master);
  1422. if (ret)
  1423. dev_err(&master->dev, "queue restart failed\n");
  1424. return ret;
  1425. }
  1426. EXPORT_SYMBOL_GPL(spi_master_resume);
  1427. static int __spi_master_match(struct device *dev, const void *data)
  1428. {
  1429. struct spi_master *m;
  1430. const u16 *bus_num = data;
  1431. m = container_of(dev, struct spi_master, dev);
  1432. return m->bus_num == *bus_num;
  1433. }
  1434. /**
  1435. * spi_busnum_to_master - look up master associated with bus_num
  1436. * @bus_num: the master's bus number
  1437. * Context: can sleep
  1438. *
  1439. * This call may be used with devices that are registered after
  1440. * arch init time. It returns a refcounted pointer to the relevant
  1441. * spi_master (which the caller must release), or NULL if there is
  1442. * no such master registered.
  1443. */
  1444. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1445. {
  1446. struct device *dev;
  1447. struct spi_master *master = NULL;
  1448. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1449. __spi_master_match);
  1450. if (dev)
  1451. master = container_of(dev, struct spi_master, dev);
  1452. /* reference got in class_find_device */
  1453. return master;
  1454. }
  1455. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1456. /*-------------------------------------------------------------------------*/
  1457. /* Core methods for SPI master protocol drivers. Some of the
  1458. * other core methods are currently defined as inline functions.
  1459. */
  1460. /**
  1461. * spi_setup - setup SPI mode and clock rate
  1462. * @spi: the device whose settings are being modified
  1463. * Context: can sleep, and no requests are queued to the device
  1464. *
  1465. * SPI protocol drivers may need to update the transfer mode if the
  1466. * device doesn't work with its default. They may likewise need
  1467. * to update clock rates or word sizes from initial values. This function
  1468. * changes those settings, and must be called from a context that can sleep.
  1469. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1470. * effect the next time the device is selected and data is transferred to
  1471. * or from it. When this function returns, the spi device is deselected.
  1472. *
  1473. * Note that this call will fail if the protocol driver specifies an option
  1474. * that the underlying controller or its driver does not support. For
  1475. * example, not all hardware supports wire transfers using nine bit words,
  1476. * LSB-first wire encoding, or active-high chipselects.
  1477. */
  1478. int spi_setup(struct spi_device *spi)
  1479. {
  1480. unsigned bad_bits;
  1481. int status = 0;
  1482. /* check mode to prevent that DUAL and QUAD set at the same time
  1483. */
  1484. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1485. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1486. dev_err(&spi->dev,
  1487. "setup: can not select dual and quad at the same time\n");
  1488. return -EINVAL;
  1489. }
  1490. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1491. */
  1492. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1493. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1494. return -EINVAL;
  1495. /* help drivers fail *cleanly* when they need options
  1496. * that aren't supported with their current master
  1497. */
  1498. bad_bits = spi->mode & ~spi->master->mode_bits;
  1499. if (bad_bits) {
  1500. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1501. bad_bits);
  1502. return -EINVAL;
  1503. }
  1504. if (!spi->bits_per_word)
  1505. spi->bits_per_word = 8;
  1506. if (!spi->max_speed_hz)
  1507. spi->max_speed_hz = spi->master->max_speed_hz;
  1508. if (spi->master->setup)
  1509. status = spi->master->setup(spi);
  1510. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1511. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1512. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1513. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1514. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1515. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1516. spi->bits_per_word, spi->max_speed_hz,
  1517. status);
  1518. return status;
  1519. }
  1520. EXPORT_SYMBOL_GPL(spi_setup);
  1521. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  1522. {
  1523. struct spi_master *master = spi->master;
  1524. struct spi_transfer *xfer;
  1525. int w_size;
  1526. if (list_empty(&message->transfers))
  1527. return -EINVAL;
  1528. /* Half-duplex links include original MicroWire, and ones with
  1529. * only one data pin like SPI_3WIRE (switches direction) or where
  1530. * either MOSI or MISO is missing. They can also be caused by
  1531. * software limitations.
  1532. */
  1533. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1534. || (spi->mode & SPI_3WIRE)) {
  1535. unsigned flags = master->flags;
  1536. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1537. if (xfer->rx_buf && xfer->tx_buf)
  1538. return -EINVAL;
  1539. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1540. return -EINVAL;
  1541. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1542. return -EINVAL;
  1543. }
  1544. }
  1545. /**
  1546. * Set transfer bits_per_word and max speed as spi device default if
  1547. * it is not set for this transfer.
  1548. * Set transfer tx_nbits and rx_nbits as single transfer default
  1549. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1550. */
  1551. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1552. message->frame_length += xfer->len;
  1553. if (!xfer->bits_per_word)
  1554. xfer->bits_per_word = spi->bits_per_word;
  1555. if (!xfer->speed_hz)
  1556. xfer->speed_hz = spi->max_speed_hz;
  1557. if (master->max_speed_hz &&
  1558. xfer->speed_hz > master->max_speed_hz)
  1559. xfer->speed_hz = master->max_speed_hz;
  1560. if (master->bits_per_word_mask) {
  1561. /* Only 32 bits fit in the mask */
  1562. if (xfer->bits_per_word > 32)
  1563. return -EINVAL;
  1564. if (!(master->bits_per_word_mask &
  1565. BIT(xfer->bits_per_word - 1)))
  1566. return -EINVAL;
  1567. }
  1568. /*
  1569. * SPI transfer length should be multiple of SPI word size
  1570. * where SPI word size should be power-of-two multiple
  1571. */
  1572. if (xfer->bits_per_word <= 8)
  1573. w_size = 1;
  1574. else if (xfer->bits_per_word <= 16)
  1575. w_size = 2;
  1576. else
  1577. w_size = 4;
  1578. /* No partial transfers accepted */
  1579. if (xfer->len % w_size)
  1580. return -EINVAL;
  1581. if (xfer->speed_hz && master->min_speed_hz &&
  1582. xfer->speed_hz < master->min_speed_hz)
  1583. return -EINVAL;
  1584. if (xfer->tx_buf && !xfer->tx_nbits)
  1585. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1586. if (xfer->rx_buf && !xfer->rx_nbits)
  1587. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1588. /* check transfer tx/rx_nbits:
  1589. * 1. check the value matches one of single, dual and quad
  1590. * 2. check tx/rx_nbits match the mode in spi_device
  1591. */
  1592. if (xfer->tx_buf) {
  1593. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1594. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1595. xfer->tx_nbits != SPI_NBITS_QUAD)
  1596. return -EINVAL;
  1597. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1598. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1599. return -EINVAL;
  1600. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1601. !(spi->mode & SPI_TX_QUAD))
  1602. return -EINVAL;
  1603. }
  1604. /* check transfer rx_nbits */
  1605. if (xfer->rx_buf) {
  1606. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1607. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1608. xfer->rx_nbits != SPI_NBITS_QUAD)
  1609. return -EINVAL;
  1610. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1611. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1612. return -EINVAL;
  1613. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1614. !(spi->mode & SPI_RX_QUAD))
  1615. return -EINVAL;
  1616. }
  1617. }
  1618. message->status = -EINPROGRESS;
  1619. return 0;
  1620. }
  1621. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1622. {
  1623. struct spi_master *master = spi->master;
  1624. message->spi = spi;
  1625. trace_spi_message_submit(message);
  1626. return master->transfer(spi, message);
  1627. }
  1628. /**
  1629. * spi_async - asynchronous SPI transfer
  1630. * @spi: device with which data will be exchanged
  1631. * @message: describes the data transfers, including completion callback
  1632. * Context: any (irqs may be blocked, etc)
  1633. *
  1634. * This call may be used in_irq and other contexts which can't sleep,
  1635. * as well as from task contexts which can sleep.
  1636. *
  1637. * The completion callback is invoked in a context which can't sleep.
  1638. * Before that invocation, the value of message->status is undefined.
  1639. * When the callback is issued, message->status holds either zero (to
  1640. * indicate complete success) or a negative error code. After that
  1641. * callback returns, the driver which issued the transfer request may
  1642. * deallocate the associated memory; it's no longer in use by any SPI
  1643. * core or controller driver code.
  1644. *
  1645. * Note that although all messages to a spi_device are handled in
  1646. * FIFO order, messages may go to different devices in other orders.
  1647. * Some device might be higher priority, or have various "hard" access
  1648. * time requirements, for example.
  1649. *
  1650. * On detection of any fault during the transfer, processing of
  1651. * the entire message is aborted, and the device is deselected.
  1652. * Until returning from the associated message completion callback,
  1653. * no other spi_message queued to that device will be processed.
  1654. * (This rule applies equally to all the synchronous transfer calls,
  1655. * which are wrappers around this core asynchronous primitive.)
  1656. */
  1657. int spi_async(struct spi_device *spi, struct spi_message *message)
  1658. {
  1659. struct spi_master *master = spi->master;
  1660. int ret;
  1661. unsigned long flags;
  1662. ret = __spi_validate(spi, message);
  1663. if (ret != 0)
  1664. return ret;
  1665. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1666. if (master->bus_lock_flag)
  1667. ret = -EBUSY;
  1668. else
  1669. ret = __spi_async(spi, message);
  1670. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1671. return ret;
  1672. }
  1673. EXPORT_SYMBOL_GPL(spi_async);
  1674. /**
  1675. * spi_async_locked - version of spi_async with exclusive bus usage
  1676. * @spi: device with which data will be exchanged
  1677. * @message: describes the data transfers, including completion callback
  1678. * Context: any (irqs may be blocked, etc)
  1679. *
  1680. * This call may be used in_irq and other contexts which can't sleep,
  1681. * as well as from task contexts which can sleep.
  1682. *
  1683. * The completion callback is invoked in a context which can't sleep.
  1684. * Before that invocation, the value of message->status is undefined.
  1685. * When the callback is issued, message->status holds either zero (to
  1686. * indicate complete success) or a negative error code. After that
  1687. * callback returns, the driver which issued the transfer request may
  1688. * deallocate the associated memory; it's no longer in use by any SPI
  1689. * core or controller driver code.
  1690. *
  1691. * Note that although all messages to a spi_device are handled in
  1692. * FIFO order, messages may go to different devices in other orders.
  1693. * Some device might be higher priority, or have various "hard" access
  1694. * time requirements, for example.
  1695. *
  1696. * On detection of any fault during the transfer, processing of
  1697. * the entire message is aborted, and the device is deselected.
  1698. * Until returning from the associated message completion callback,
  1699. * no other spi_message queued to that device will be processed.
  1700. * (This rule applies equally to all the synchronous transfer calls,
  1701. * which are wrappers around this core asynchronous primitive.)
  1702. */
  1703. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1704. {
  1705. struct spi_master *master = spi->master;
  1706. int ret;
  1707. unsigned long flags;
  1708. ret = __spi_validate(spi, message);
  1709. if (ret != 0)
  1710. return ret;
  1711. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1712. ret = __spi_async(spi, message);
  1713. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1714. return ret;
  1715. }
  1716. EXPORT_SYMBOL_GPL(spi_async_locked);
  1717. /*-------------------------------------------------------------------------*/
  1718. /* Utility methods for SPI master protocol drivers, layered on
  1719. * top of the core. Some other utility methods are defined as
  1720. * inline functions.
  1721. */
  1722. static void spi_complete(void *arg)
  1723. {
  1724. complete(arg);
  1725. }
  1726. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1727. int bus_locked)
  1728. {
  1729. DECLARE_COMPLETION_ONSTACK(done);
  1730. int status;
  1731. struct spi_master *master = spi->master;
  1732. message->complete = spi_complete;
  1733. message->context = &done;
  1734. if (!bus_locked)
  1735. mutex_lock(&master->bus_lock_mutex);
  1736. status = spi_async_locked(spi, message);
  1737. if (!bus_locked)
  1738. mutex_unlock(&master->bus_lock_mutex);
  1739. if (status == 0) {
  1740. wait_for_completion(&done);
  1741. status = message->status;
  1742. }
  1743. message->context = NULL;
  1744. return status;
  1745. }
  1746. /**
  1747. * spi_sync - blocking/synchronous SPI data transfers
  1748. * @spi: device with which data will be exchanged
  1749. * @message: describes the data transfers
  1750. * Context: can sleep
  1751. *
  1752. * This call may only be used from a context that may sleep. The sleep
  1753. * is non-interruptible, and has no timeout. Low-overhead controller
  1754. * drivers may DMA directly into and out of the message buffers.
  1755. *
  1756. * Note that the SPI device's chip select is active during the message,
  1757. * and then is normally disabled between messages. Drivers for some
  1758. * frequently-used devices may want to minimize costs of selecting a chip,
  1759. * by leaving it selected in anticipation that the next message will go
  1760. * to the same chip. (That may increase power usage.)
  1761. *
  1762. * Also, the caller is guaranteeing that the memory associated with the
  1763. * message will not be freed before this call returns.
  1764. *
  1765. * It returns zero on success, else a negative error code.
  1766. */
  1767. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1768. {
  1769. return __spi_sync(spi, message, 0);
  1770. }
  1771. EXPORT_SYMBOL_GPL(spi_sync);
  1772. /**
  1773. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1774. * @spi: device with which data will be exchanged
  1775. * @message: describes the data transfers
  1776. * Context: can sleep
  1777. *
  1778. * This call may only be used from a context that may sleep. The sleep
  1779. * is non-interruptible, and has no timeout. Low-overhead controller
  1780. * drivers may DMA directly into and out of the message buffers.
  1781. *
  1782. * This call should be used by drivers that require exclusive access to the
  1783. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1784. * be released by a spi_bus_unlock call when the exclusive access is over.
  1785. *
  1786. * It returns zero on success, else a negative error code.
  1787. */
  1788. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1789. {
  1790. return __spi_sync(spi, message, 1);
  1791. }
  1792. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1793. /**
  1794. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1795. * @master: SPI bus master that should be locked for exclusive bus access
  1796. * Context: can sleep
  1797. *
  1798. * This call may only be used from a context that may sleep. The sleep
  1799. * is non-interruptible, and has no timeout.
  1800. *
  1801. * This call should be used by drivers that require exclusive access to the
  1802. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1803. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1804. * and spi_async_locked calls when the SPI bus lock is held.
  1805. *
  1806. * It returns zero on success, else a negative error code.
  1807. */
  1808. int spi_bus_lock(struct spi_master *master)
  1809. {
  1810. unsigned long flags;
  1811. mutex_lock(&master->bus_lock_mutex);
  1812. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1813. master->bus_lock_flag = 1;
  1814. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1815. /* mutex remains locked until spi_bus_unlock is called */
  1816. return 0;
  1817. }
  1818. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1819. /**
  1820. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1821. * @master: SPI bus master that was locked for exclusive bus access
  1822. * Context: can sleep
  1823. *
  1824. * This call may only be used from a context that may sleep. The sleep
  1825. * is non-interruptible, and has no timeout.
  1826. *
  1827. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1828. * call.
  1829. *
  1830. * It returns zero on success, else a negative error code.
  1831. */
  1832. int spi_bus_unlock(struct spi_master *master)
  1833. {
  1834. master->bus_lock_flag = 0;
  1835. mutex_unlock(&master->bus_lock_mutex);
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1839. /* portable code must never pass more than 32 bytes */
  1840. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1841. static u8 *buf;
  1842. /**
  1843. * spi_write_then_read - SPI synchronous write followed by read
  1844. * @spi: device with which data will be exchanged
  1845. * @txbuf: data to be written (need not be dma-safe)
  1846. * @n_tx: size of txbuf, in bytes
  1847. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1848. * @n_rx: size of rxbuf, in bytes
  1849. * Context: can sleep
  1850. *
  1851. * This performs a half duplex MicroWire style transaction with the
  1852. * device, sending txbuf and then reading rxbuf. The return value
  1853. * is zero for success, else a negative errno status code.
  1854. * This call may only be used from a context that may sleep.
  1855. *
  1856. * Parameters to this routine are always copied using a small buffer;
  1857. * portable code should never use this for more than 32 bytes.
  1858. * Performance-sensitive or bulk transfer code should instead use
  1859. * spi_{async,sync}() calls with dma-safe buffers.
  1860. */
  1861. int spi_write_then_read(struct spi_device *spi,
  1862. const void *txbuf, unsigned n_tx,
  1863. void *rxbuf, unsigned n_rx)
  1864. {
  1865. static DEFINE_MUTEX(lock);
  1866. int status;
  1867. struct spi_message message;
  1868. struct spi_transfer x[2];
  1869. u8 *local_buf;
  1870. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1871. * copying here, (as a pure convenience thing), but we can
  1872. * keep heap costs out of the hot path unless someone else is
  1873. * using the pre-allocated buffer or the transfer is too large.
  1874. */
  1875. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1876. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1877. GFP_KERNEL | GFP_DMA);
  1878. if (!local_buf)
  1879. return -ENOMEM;
  1880. } else {
  1881. local_buf = buf;
  1882. }
  1883. spi_message_init(&message);
  1884. memset(x, 0, sizeof(x));
  1885. if (n_tx) {
  1886. x[0].len = n_tx;
  1887. spi_message_add_tail(&x[0], &message);
  1888. }
  1889. if (n_rx) {
  1890. x[1].len = n_rx;
  1891. spi_message_add_tail(&x[1], &message);
  1892. }
  1893. memcpy(local_buf, txbuf, n_tx);
  1894. x[0].tx_buf = local_buf;
  1895. x[1].rx_buf = local_buf + n_tx;
  1896. /* do the i/o */
  1897. status = spi_sync(spi, &message);
  1898. if (status == 0)
  1899. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1900. if (x[0].tx_buf == buf)
  1901. mutex_unlock(&lock);
  1902. else
  1903. kfree(local_buf);
  1904. return status;
  1905. }
  1906. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1907. /*-------------------------------------------------------------------------*/
  1908. static int __init spi_init(void)
  1909. {
  1910. int status;
  1911. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1912. if (!buf) {
  1913. status = -ENOMEM;
  1914. goto err0;
  1915. }
  1916. status = bus_register(&spi_bus_type);
  1917. if (status < 0)
  1918. goto err1;
  1919. status = class_register(&spi_master_class);
  1920. if (status < 0)
  1921. goto err2;
  1922. return 0;
  1923. err2:
  1924. bus_unregister(&spi_bus_type);
  1925. err1:
  1926. kfree(buf);
  1927. buf = NULL;
  1928. err0:
  1929. return status;
  1930. }
  1931. /* board_info is normally registered in arch_initcall(),
  1932. * but even essential drivers wait till later
  1933. *
  1934. * REVISIT only boardinfo really needs static linking. the rest (device and
  1935. * driver registration) _could_ be dynamically linked (modular) ... costs
  1936. * include needing to have boardinfo data structures be much more public.
  1937. */
  1938. postcore_initcall(spi_init);