spi.c 63 KB

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