spi.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  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 - function which processes spi message queue
  732. * @master: master to process queue for
  733. * @in_kthread: true if we are in the context of the message pump thread
  734. *
  735. * This function checks if there is any spi message in the queue that
  736. * needs processing and if so call out to the driver to initialize hardware
  737. * and transfer each message.
  738. *
  739. * Note that it is called both from the kthread itself and also from
  740. * inside spi_sync(); the queue extraction handling at the top of the
  741. * function should deal with this safely.
  742. */
  743. static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
  744. {
  745. unsigned long flags;
  746. bool was_busy = false;
  747. int ret;
  748. /* Lock queue */
  749. spin_lock_irqsave(&master->queue_lock, flags);
  750. /* Make sure we are not already running a message */
  751. if (master->cur_msg) {
  752. spin_unlock_irqrestore(&master->queue_lock, flags);
  753. return;
  754. }
  755. /* If another context is idling the device then defer */
  756. if (master->idling) {
  757. queue_kthread_work(&master->kworker, &master->pump_messages);
  758. spin_unlock_irqrestore(&master->queue_lock, flags);
  759. return;
  760. }
  761. /* Check if the queue is idle */
  762. if (list_empty(&master->queue) || !master->running) {
  763. if (!master->busy) {
  764. spin_unlock_irqrestore(&master->queue_lock, flags);
  765. return;
  766. }
  767. /* Only do teardown in the thread */
  768. if (!in_kthread) {
  769. queue_kthread_work(&master->kworker,
  770. &master->pump_messages);
  771. spin_unlock_irqrestore(&master->queue_lock, flags);
  772. return;
  773. }
  774. master->busy = false;
  775. master->idling = true;
  776. spin_unlock_irqrestore(&master->queue_lock, flags);
  777. kfree(master->dummy_rx);
  778. master->dummy_rx = NULL;
  779. kfree(master->dummy_tx);
  780. master->dummy_tx = NULL;
  781. if (master->unprepare_transfer_hardware &&
  782. master->unprepare_transfer_hardware(master))
  783. dev_err(&master->dev,
  784. "failed to unprepare transfer hardware\n");
  785. if (master->auto_runtime_pm) {
  786. pm_runtime_mark_last_busy(master->dev.parent);
  787. pm_runtime_put_autosuspend(master->dev.parent);
  788. }
  789. trace_spi_master_idle(master);
  790. spin_lock_irqsave(&master->queue_lock, flags);
  791. master->idling = false;
  792. spin_unlock_irqrestore(&master->queue_lock, flags);
  793. return;
  794. }
  795. /* Extract head of queue */
  796. master->cur_msg =
  797. list_first_entry(&master->queue, struct spi_message, queue);
  798. list_del_init(&master->cur_msg->queue);
  799. if (master->busy)
  800. was_busy = true;
  801. else
  802. master->busy = true;
  803. spin_unlock_irqrestore(&master->queue_lock, flags);
  804. if (!was_busy && master->auto_runtime_pm) {
  805. ret = pm_runtime_get_sync(master->dev.parent);
  806. if (ret < 0) {
  807. dev_err(&master->dev, "Failed to power device: %d\n",
  808. ret);
  809. return;
  810. }
  811. }
  812. if (!was_busy)
  813. trace_spi_master_busy(master);
  814. if (!was_busy && master->prepare_transfer_hardware) {
  815. ret = master->prepare_transfer_hardware(master);
  816. if (ret) {
  817. dev_err(&master->dev,
  818. "failed to prepare transfer hardware\n");
  819. if (master->auto_runtime_pm)
  820. pm_runtime_put(master->dev.parent);
  821. return;
  822. }
  823. }
  824. trace_spi_message_start(master->cur_msg);
  825. if (master->prepare_message) {
  826. ret = master->prepare_message(master, master->cur_msg);
  827. if (ret) {
  828. dev_err(&master->dev,
  829. "failed to prepare message: %d\n", ret);
  830. master->cur_msg->status = ret;
  831. spi_finalize_current_message(master);
  832. return;
  833. }
  834. master->cur_msg_prepared = true;
  835. }
  836. ret = spi_map_msg(master, master->cur_msg);
  837. if (ret) {
  838. master->cur_msg->status = ret;
  839. spi_finalize_current_message(master);
  840. return;
  841. }
  842. ret = master->transfer_one_message(master, master->cur_msg);
  843. if (ret) {
  844. dev_err(&master->dev,
  845. "failed to transfer one message from queue\n");
  846. return;
  847. }
  848. }
  849. /**
  850. * spi_pump_messages - kthread work function which processes spi message queue
  851. * @work: pointer to kthread work struct contained in the master struct
  852. */
  853. static void spi_pump_messages(struct kthread_work *work)
  854. {
  855. struct spi_master *master =
  856. container_of(work, struct spi_master, pump_messages);
  857. __spi_pump_messages(master, true);
  858. }
  859. static int spi_init_queue(struct spi_master *master)
  860. {
  861. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  862. master->running = false;
  863. master->busy = false;
  864. init_kthread_worker(&master->kworker);
  865. master->kworker_task = kthread_run(kthread_worker_fn,
  866. &master->kworker, "%s",
  867. dev_name(&master->dev));
  868. if (IS_ERR(master->kworker_task)) {
  869. dev_err(&master->dev, "failed to create message pump task\n");
  870. return PTR_ERR(master->kworker_task);
  871. }
  872. init_kthread_work(&master->pump_messages, spi_pump_messages);
  873. /*
  874. * Master config will indicate if this controller should run the
  875. * message pump with high (realtime) priority to reduce the transfer
  876. * latency on the bus by minimising the delay between a transfer
  877. * request and the scheduling of the message pump thread. Without this
  878. * setting the message pump thread will remain at default priority.
  879. */
  880. if (master->rt) {
  881. dev_info(&master->dev,
  882. "will run message pump with realtime priority\n");
  883. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  884. }
  885. return 0;
  886. }
  887. /**
  888. * spi_get_next_queued_message() - called by driver to check for queued
  889. * messages
  890. * @master: the master to check for queued messages
  891. *
  892. * If there are more messages in the queue, the next message is returned from
  893. * this call.
  894. */
  895. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  896. {
  897. struct spi_message *next;
  898. unsigned long flags;
  899. /* get a pointer to the next message, if any */
  900. spin_lock_irqsave(&master->queue_lock, flags);
  901. next = list_first_entry_or_null(&master->queue, struct spi_message,
  902. queue);
  903. spin_unlock_irqrestore(&master->queue_lock, flags);
  904. return next;
  905. }
  906. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  907. /**
  908. * spi_finalize_current_message() - the current message is complete
  909. * @master: the master to return the message to
  910. *
  911. * Called by the driver to notify the core that the message in the front of the
  912. * queue is complete and can be removed from the queue.
  913. */
  914. void spi_finalize_current_message(struct spi_master *master)
  915. {
  916. struct spi_message *mesg;
  917. unsigned long flags;
  918. int ret;
  919. spin_lock_irqsave(&master->queue_lock, flags);
  920. mesg = master->cur_msg;
  921. master->cur_msg = NULL;
  922. queue_kthread_work(&master->kworker, &master->pump_messages);
  923. spin_unlock_irqrestore(&master->queue_lock, flags);
  924. spi_unmap_msg(master, mesg);
  925. if (master->cur_msg_prepared && master->unprepare_message) {
  926. ret = master->unprepare_message(master, mesg);
  927. if (ret) {
  928. dev_err(&master->dev,
  929. "failed to unprepare message: %d\n", ret);
  930. }
  931. }
  932. master->cur_msg_prepared = false;
  933. mesg->state = NULL;
  934. if (mesg->complete)
  935. mesg->complete(mesg->context);
  936. trace_spi_message_done(mesg);
  937. }
  938. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  939. static int spi_start_queue(struct spi_master *master)
  940. {
  941. unsigned long flags;
  942. spin_lock_irqsave(&master->queue_lock, flags);
  943. if (master->running || master->busy) {
  944. spin_unlock_irqrestore(&master->queue_lock, flags);
  945. return -EBUSY;
  946. }
  947. master->running = true;
  948. master->cur_msg = NULL;
  949. spin_unlock_irqrestore(&master->queue_lock, flags);
  950. queue_kthread_work(&master->kworker, &master->pump_messages);
  951. return 0;
  952. }
  953. static int spi_stop_queue(struct spi_master *master)
  954. {
  955. unsigned long flags;
  956. unsigned limit = 500;
  957. int ret = 0;
  958. spin_lock_irqsave(&master->queue_lock, flags);
  959. /*
  960. * This is a bit lame, but is optimized for the common execution path.
  961. * A wait_queue on the master->busy could be used, but then the common
  962. * execution path (pump_messages) would be required to call wake_up or
  963. * friends on every SPI message. Do this instead.
  964. */
  965. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  966. spin_unlock_irqrestore(&master->queue_lock, flags);
  967. usleep_range(10000, 11000);
  968. spin_lock_irqsave(&master->queue_lock, flags);
  969. }
  970. if (!list_empty(&master->queue) || master->busy)
  971. ret = -EBUSY;
  972. else
  973. master->running = false;
  974. spin_unlock_irqrestore(&master->queue_lock, flags);
  975. if (ret) {
  976. dev_warn(&master->dev,
  977. "could not stop message queue\n");
  978. return ret;
  979. }
  980. return ret;
  981. }
  982. static int spi_destroy_queue(struct spi_master *master)
  983. {
  984. int ret;
  985. ret = spi_stop_queue(master);
  986. /*
  987. * flush_kthread_worker will block until all work is done.
  988. * If the reason that stop_queue timed out is that the work will never
  989. * finish, then it does no good to call flush/stop thread, so
  990. * return anyway.
  991. */
  992. if (ret) {
  993. dev_err(&master->dev, "problem destroying queue\n");
  994. return ret;
  995. }
  996. flush_kthread_worker(&master->kworker);
  997. kthread_stop(master->kworker_task);
  998. return 0;
  999. }
  1000. static int __spi_queued_transfer(struct spi_device *spi,
  1001. struct spi_message *msg,
  1002. bool need_pump)
  1003. {
  1004. struct spi_master *master = spi->master;
  1005. unsigned long flags;
  1006. spin_lock_irqsave(&master->queue_lock, flags);
  1007. if (!master->running) {
  1008. spin_unlock_irqrestore(&master->queue_lock, flags);
  1009. return -ESHUTDOWN;
  1010. }
  1011. msg->actual_length = 0;
  1012. msg->status = -EINPROGRESS;
  1013. list_add_tail(&msg->queue, &master->queue);
  1014. if (!master->busy && need_pump)
  1015. queue_kthread_work(&master->kworker, &master->pump_messages);
  1016. spin_unlock_irqrestore(&master->queue_lock, flags);
  1017. return 0;
  1018. }
  1019. /**
  1020. * spi_queued_transfer - transfer function for queued transfers
  1021. * @spi: spi device which is requesting transfer
  1022. * @msg: spi message which is to handled is queued to driver queue
  1023. */
  1024. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  1025. {
  1026. return __spi_queued_transfer(spi, msg, true);
  1027. }
  1028. static int spi_master_initialize_queue(struct spi_master *master)
  1029. {
  1030. int ret;
  1031. master->transfer = spi_queued_transfer;
  1032. if (!master->transfer_one_message)
  1033. master->transfer_one_message = spi_transfer_one_message;
  1034. /* Initialize and start queue */
  1035. ret = spi_init_queue(master);
  1036. if (ret) {
  1037. dev_err(&master->dev, "problem initializing queue\n");
  1038. goto err_init_queue;
  1039. }
  1040. master->queued = true;
  1041. ret = spi_start_queue(master);
  1042. if (ret) {
  1043. dev_err(&master->dev, "problem starting queue\n");
  1044. goto err_start_queue;
  1045. }
  1046. return 0;
  1047. err_start_queue:
  1048. spi_destroy_queue(master);
  1049. err_init_queue:
  1050. return ret;
  1051. }
  1052. /*-------------------------------------------------------------------------*/
  1053. #if defined(CONFIG_OF)
  1054. static struct spi_device *
  1055. of_register_spi_device(struct spi_master *master, struct device_node *nc)
  1056. {
  1057. struct spi_device *spi;
  1058. int rc;
  1059. u32 value;
  1060. /* Alloc an spi_device */
  1061. spi = spi_alloc_device(master);
  1062. if (!spi) {
  1063. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1064. nc->full_name);
  1065. rc = -ENOMEM;
  1066. goto err_out;
  1067. }
  1068. /* Select device driver */
  1069. rc = of_modalias_node(nc, spi->modalias,
  1070. sizeof(spi->modalias));
  1071. if (rc < 0) {
  1072. dev_err(&master->dev, "cannot find modalias for %s\n",
  1073. nc->full_name);
  1074. goto err_out;
  1075. }
  1076. /* Device address */
  1077. rc = of_property_read_u32(nc, "reg", &value);
  1078. if (rc) {
  1079. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1080. nc->full_name, rc);
  1081. goto err_out;
  1082. }
  1083. spi->chip_select = value;
  1084. /* Mode (clock phase/polarity/etc.) */
  1085. if (of_find_property(nc, "spi-cpha", NULL))
  1086. spi->mode |= SPI_CPHA;
  1087. if (of_find_property(nc, "spi-cpol", NULL))
  1088. spi->mode |= SPI_CPOL;
  1089. if (of_find_property(nc, "spi-cs-high", NULL))
  1090. spi->mode |= SPI_CS_HIGH;
  1091. if (of_find_property(nc, "spi-3wire", NULL))
  1092. spi->mode |= SPI_3WIRE;
  1093. if (of_find_property(nc, "spi-lsb-first", NULL))
  1094. spi->mode |= SPI_LSB_FIRST;
  1095. /* Device DUAL/QUAD mode */
  1096. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1097. switch (value) {
  1098. case 1:
  1099. break;
  1100. case 2:
  1101. spi->mode |= SPI_TX_DUAL;
  1102. break;
  1103. case 4:
  1104. spi->mode |= SPI_TX_QUAD;
  1105. break;
  1106. default:
  1107. dev_warn(&master->dev,
  1108. "spi-tx-bus-width %d not supported\n",
  1109. value);
  1110. break;
  1111. }
  1112. }
  1113. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1114. switch (value) {
  1115. case 1:
  1116. break;
  1117. case 2:
  1118. spi->mode |= SPI_RX_DUAL;
  1119. break;
  1120. case 4:
  1121. spi->mode |= SPI_RX_QUAD;
  1122. break;
  1123. default:
  1124. dev_warn(&master->dev,
  1125. "spi-rx-bus-width %d not supported\n",
  1126. value);
  1127. break;
  1128. }
  1129. }
  1130. /* Device speed */
  1131. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1132. if (rc) {
  1133. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1134. nc->full_name, rc);
  1135. goto err_out;
  1136. }
  1137. spi->max_speed_hz = value;
  1138. /* IRQ */
  1139. spi->irq = irq_of_parse_and_map(nc, 0);
  1140. /* Store a pointer to the node in the device structure */
  1141. of_node_get(nc);
  1142. spi->dev.of_node = nc;
  1143. /* Register the new device */
  1144. request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
  1145. rc = spi_add_device(spi);
  1146. if (rc) {
  1147. dev_err(&master->dev, "spi_device register error %s\n",
  1148. nc->full_name);
  1149. goto err_out;
  1150. }
  1151. return spi;
  1152. err_out:
  1153. spi_dev_put(spi);
  1154. return ERR_PTR(rc);
  1155. }
  1156. /**
  1157. * of_register_spi_devices() - Register child devices onto the SPI bus
  1158. * @master: Pointer to spi_master device
  1159. *
  1160. * Registers an spi_device for each child node of master node which has a 'reg'
  1161. * property.
  1162. */
  1163. static void of_register_spi_devices(struct spi_master *master)
  1164. {
  1165. struct spi_device *spi;
  1166. struct device_node *nc;
  1167. if (!master->dev.of_node)
  1168. return;
  1169. for_each_available_child_of_node(master->dev.of_node, nc) {
  1170. spi = of_register_spi_device(master, nc);
  1171. if (IS_ERR(spi))
  1172. dev_warn(&master->dev, "Failed to create SPI device for %s\n",
  1173. nc->full_name);
  1174. }
  1175. }
  1176. #else
  1177. static void of_register_spi_devices(struct spi_master *master) { }
  1178. #endif
  1179. #ifdef CONFIG_ACPI
  1180. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1181. {
  1182. struct spi_device *spi = data;
  1183. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1184. struct acpi_resource_spi_serialbus *sb;
  1185. sb = &ares->data.spi_serial_bus;
  1186. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1187. spi->chip_select = sb->device_selection;
  1188. spi->max_speed_hz = sb->connection_speed;
  1189. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1190. spi->mode |= SPI_CPHA;
  1191. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1192. spi->mode |= SPI_CPOL;
  1193. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1194. spi->mode |= SPI_CS_HIGH;
  1195. }
  1196. } else if (spi->irq < 0) {
  1197. struct resource r;
  1198. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1199. spi->irq = r.start;
  1200. }
  1201. /* Always tell the ACPI core to skip this resource */
  1202. return 1;
  1203. }
  1204. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1205. void *data, void **return_value)
  1206. {
  1207. struct spi_master *master = data;
  1208. struct list_head resource_list;
  1209. struct acpi_device *adev;
  1210. struct spi_device *spi;
  1211. int ret;
  1212. if (acpi_bus_get_device(handle, &adev))
  1213. return AE_OK;
  1214. if (acpi_bus_get_status(adev) || !adev->status.present)
  1215. return AE_OK;
  1216. spi = spi_alloc_device(master);
  1217. if (!spi) {
  1218. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1219. dev_name(&adev->dev));
  1220. return AE_NO_MEMORY;
  1221. }
  1222. ACPI_COMPANION_SET(&spi->dev, adev);
  1223. spi->irq = -1;
  1224. INIT_LIST_HEAD(&resource_list);
  1225. ret = acpi_dev_get_resources(adev, &resource_list,
  1226. acpi_spi_add_resource, spi);
  1227. acpi_dev_free_resource_list(&resource_list);
  1228. if (ret < 0 || !spi->max_speed_hz) {
  1229. spi_dev_put(spi);
  1230. return AE_OK;
  1231. }
  1232. adev->power.flags.ignore_parent = true;
  1233. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1234. if (spi_add_device(spi)) {
  1235. adev->power.flags.ignore_parent = false;
  1236. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1237. dev_name(&adev->dev));
  1238. spi_dev_put(spi);
  1239. }
  1240. return AE_OK;
  1241. }
  1242. static void acpi_register_spi_devices(struct spi_master *master)
  1243. {
  1244. acpi_status status;
  1245. acpi_handle handle;
  1246. handle = ACPI_HANDLE(master->dev.parent);
  1247. if (!handle)
  1248. return;
  1249. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1250. acpi_spi_add_device, NULL,
  1251. master, NULL);
  1252. if (ACPI_FAILURE(status))
  1253. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1254. }
  1255. #else
  1256. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1257. #endif /* CONFIG_ACPI */
  1258. static void spi_master_release(struct device *dev)
  1259. {
  1260. struct spi_master *master;
  1261. master = container_of(dev, struct spi_master, dev);
  1262. kfree(master);
  1263. }
  1264. static struct class spi_master_class = {
  1265. .name = "spi_master",
  1266. .owner = THIS_MODULE,
  1267. .dev_release = spi_master_release,
  1268. };
  1269. /**
  1270. * spi_alloc_master - allocate SPI master controller
  1271. * @dev: the controller, possibly using the platform_bus
  1272. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1273. * memory is in the driver_data field of the returned device,
  1274. * accessible with spi_master_get_devdata().
  1275. * Context: can sleep
  1276. *
  1277. * This call is used only by SPI master controller drivers, which are the
  1278. * only ones directly touching chip registers. It's how they allocate
  1279. * an spi_master structure, prior to calling spi_register_master().
  1280. *
  1281. * This must be called from context that can sleep. It returns the SPI
  1282. * master structure on success, else NULL.
  1283. *
  1284. * The caller is responsible for assigning the bus number and initializing
  1285. * the master's methods before calling spi_register_master(); and (after errors
  1286. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1287. * leak.
  1288. */
  1289. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1290. {
  1291. struct spi_master *master;
  1292. if (!dev)
  1293. return NULL;
  1294. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1295. if (!master)
  1296. return NULL;
  1297. device_initialize(&master->dev);
  1298. master->bus_num = -1;
  1299. master->num_chipselect = 1;
  1300. master->dev.class = &spi_master_class;
  1301. master->dev.parent = get_device(dev);
  1302. spi_master_set_devdata(master, &master[1]);
  1303. return master;
  1304. }
  1305. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1306. #ifdef CONFIG_OF
  1307. static int of_spi_register_master(struct spi_master *master)
  1308. {
  1309. int nb, i, *cs;
  1310. struct device_node *np = master->dev.of_node;
  1311. if (!np)
  1312. return 0;
  1313. nb = of_gpio_named_count(np, "cs-gpios");
  1314. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1315. /* Return error only for an incorrectly formed cs-gpios property */
  1316. if (nb == 0 || nb == -ENOENT)
  1317. return 0;
  1318. else if (nb < 0)
  1319. return nb;
  1320. cs = devm_kzalloc(&master->dev,
  1321. sizeof(int) * master->num_chipselect,
  1322. GFP_KERNEL);
  1323. master->cs_gpios = cs;
  1324. if (!master->cs_gpios)
  1325. return -ENOMEM;
  1326. for (i = 0; i < master->num_chipselect; i++)
  1327. cs[i] = -ENOENT;
  1328. for (i = 0; i < nb; i++)
  1329. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1330. return 0;
  1331. }
  1332. #else
  1333. static int of_spi_register_master(struct spi_master *master)
  1334. {
  1335. return 0;
  1336. }
  1337. #endif
  1338. /**
  1339. * spi_register_master - register SPI master controller
  1340. * @master: initialized master, originally from spi_alloc_master()
  1341. * Context: can sleep
  1342. *
  1343. * SPI master controllers connect to their drivers using some non-SPI bus,
  1344. * such as the platform bus. The final stage of probe() in that code
  1345. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1346. *
  1347. * SPI controllers use board specific (often SOC specific) bus numbers,
  1348. * and board-specific addressing for SPI devices combines those numbers
  1349. * with chip select numbers. Since SPI does not directly support dynamic
  1350. * device identification, boards need configuration tables telling which
  1351. * chip is at which address.
  1352. *
  1353. * This must be called from context that can sleep. It returns zero on
  1354. * success, else a negative error code (dropping the master's refcount).
  1355. * After a successful return, the caller is responsible for calling
  1356. * spi_unregister_master().
  1357. */
  1358. int spi_register_master(struct spi_master *master)
  1359. {
  1360. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1361. struct device *dev = master->dev.parent;
  1362. struct boardinfo *bi;
  1363. int status = -ENODEV;
  1364. int dynamic = 0;
  1365. if (!dev)
  1366. return -ENODEV;
  1367. status = of_spi_register_master(master);
  1368. if (status)
  1369. return status;
  1370. /* even if it's just one always-selected device, there must
  1371. * be at least one chipselect
  1372. */
  1373. if (master->num_chipselect == 0)
  1374. return -EINVAL;
  1375. if ((master->bus_num < 0) && master->dev.of_node)
  1376. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1377. /* convention: dynamically assigned bus IDs count down from the max */
  1378. if (master->bus_num < 0) {
  1379. /* FIXME switch to an IDR based scheme, something like
  1380. * I2C now uses, so we can't run out of "dynamic" IDs
  1381. */
  1382. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1383. dynamic = 1;
  1384. }
  1385. INIT_LIST_HEAD(&master->queue);
  1386. spin_lock_init(&master->queue_lock);
  1387. spin_lock_init(&master->bus_lock_spinlock);
  1388. mutex_init(&master->bus_lock_mutex);
  1389. master->bus_lock_flag = 0;
  1390. init_completion(&master->xfer_completion);
  1391. if (!master->max_dma_len)
  1392. master->max_dma_len = INT_MAX;
  1393. /* register the device, then userspace will see it.
  1394. * registration fails if the bus ID is in use.
  1395. */
  1396. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1397. status = device_add(&master->dev);
  1398. if (status < 0)
  1399. goto done;
  1400. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1401. dynamic ? " (dynamic)" : "");
  1402. /* If we're using a queued driver, start the queue */
  1403. if (master->transfer)
  1404. dev_info(dev, "master is unqueued, this is deprecated\n");
  1405. else {
  1406. status = spi_master_initialize_queue(master);
  1407. if (status) {
  1408. device_del(&master->dev);
  1409. goto done;
  1410. }
  1411. }
  1412. mutex_lock(&board_lock);
  1413. list_add_tail(&master->list, &spi_master_list);
  1414. list_for_each_entry(bi, &board_list, list)
  1415. spi_match_master_to_boardinfo(master, &bi->board_info);
  1416. mutex_unlock(&board_lock);
  1417. /* Register devices from the device tree and ACPI */
  1418. of_register_spi_devices(master);
  1419. acpi_register_spi_devices(master);
  1420. done:
  1421. return status;
  1422. }
  1423. EXPORT_SYMBOL_GPL(spi_register_master);
  1424. static void devm_spi_unregister(struct device *dev, void *res)
  1425. {
  1426. spi_unregister_master(*(struct spi_master **)res);
  1427. }
  1428. /**
  1429. * dev_spi_register_master - register managed SPI master controller
  1430. * @dev: device managing SPI master
  1431. * @master: initialized master, originally from spi_alloc_master()
  1432. * Context: can sleep
  1433. *
  1434. * Register a SPI device as with spi_register_master() which will
  1435. * automatically be unregister
  1436. */
  1437. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1438. {
  1439. struct spi_master **ptr;
  1440. int ret;
  1441. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1442. if (!ptr)
  1443. return -ENOMEM;
  1444. ret = spi_register_master(master);
  1445. if (!ret) {
  1446. *ptr = master;
  1447. devres_add(dev, ptr);
  1448. } else {
  1449. devres_free(ptr);
  1450. }
  1451. return ret;
  1452. }
  1453. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1454. static int __unregister(struct device *dev, void *null)
  1455. {
  1456. spi_unregister_device(to_spi_device(dev));
  1457. return 0;
  1458. }
  1459. /**
  1460. * spi_unregister_master - unregister SPI master controller
  1461. * @master: the master being unregistered
  1462. * Context: can sleep
  1463. *
  1464. * This call is used only by SPI master controller drivers, which are the
  1465. * only ones directly touching chip registers.
  1466. *
  1467. * This must be called from context that can sleep.
  1468. */
  1469. void spi_unregister_master(struct spi_master *master)
  1470. {
  1471. int dummy;
  1472. if (master->queued) {
  1473. if (spi_destroy_queue(master))
  1474. dev_err(&master->dev, "queue remove failed\n");
  1475. }
  1476. mutex_lock(&board_lock);
  1477. list_del(&master->list);
  1478. mutex_unlock(&board_lock);
  1479. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1480. device_unregister(&master->dev);
  1481. }
  1482. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1483. int spi_master_suspend(struct spi_master *master)
  1484. {
  1485. int ret;
  1486. /* Basically no-ops for non-queued masters */
  1487. if (!master->queued)
  1488. return 0;
  1489. ret = spi_stop_queue(master);
  1490. if (ret)
  1491. dev_err(&master->dev, "queue stop failed\n");
  1492. return ret;
  1493. }
  1494. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1495. int spi_master_resume(struct spi_master *master)
  1496. {
  1497. int ret;
  1498. if (!master->queued)
  1499. return 0;
  1500. ret = spi_start_queue(master);
  1501. if (ret)
  1502. dev_err(&master->dev, "queue restart failed\n");
  1503. return ret;
  1504. }
  1505. EXPORT_SYMBOL_GPL(spi_master_resume);
  1506. static int __spi_master_match(struct device *dev, const void *data)
  1507. {
  1508. struct spi_master *m;
  1509. const u16 *bus_num = data;
  1510. m = container_of(dev, struct spi_master, dev);
  1511. return m->bus_num == *bus_num;
  1512. }
  1513. /**
  1514. * spi_busnum_to_master - look up master associated with bus_num
  1515. * @bus_num: the master's bus number
  1516. * Context: can sleep
  1517. *
  1518. * This call may be used with devices that are registered after
  1519. * arch init time. It returns a refcounted pointer to the relevant
  1520. * spi_master (which the caller must release), or NULL if there is
  1521. * no such master registered.
  1522. */
  1523. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1524. {
  1525. struct device *dev;
  1526. struct spi_master *master = NULL;
  1527. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1528. __spi_master_match);
  1529. if (dev)
  1530. master = container_of(dev, struct spi_master, dev);
  1531. /* reference got in class_find_device */
  1532. return master;
  1533. }
  1534. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1535. /*-------------------------------------------------------------------------*/
  1536. /* Core methods for SPI master protocol drivers. Some of the
  1537. * other core methods are currently defined as inline functions.
  1538. */
  1539. /**
  1540. * spi_setup - setup SPI mode and clock rate
  1541. * @spi: the device whose settings are being modified
  1542. * Context: can sleep, and no requests are queued to the device
  1543. *
  1544. * SPI protocol drivers may need to update the transfer mode if the
  1545. * device doesn't work with its default. They may likewise need
  1546. * to update clock rates or word sizes from initial values. This function
  1547. * changes those settings, and must be called from a context that can sleep.
  1548. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1549. * effect the next time the device is selected and data is transferred to
  1550. * or from it. When this function returns, the spi device is deselected.
  1551. *
  1552. * Note that this call will fail if the protocol driver specifies an option
  1553. * that the underlying controller or its driver does not support. For
  1554. * example, not all hardware supports wire transfers using nine bit words,
  1555. * LSB-first wire encoding, or active-high chipselects.
  1556. */
  1557. int spi_setup(struct spi_device *spi)
  1558. {
  1559. unsigned bad_bits, ugly_bits;
  1560. int status = 0;
  1561. /* check mode to prevent that DUAL and QUAD set at the same time
  1562. */
  1563. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1564. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1565. dev_err(&spi->dev,
  1566. "setup: can not select dual and quad at the same time\n");
  1567. return -EINVAL;
  1568. }
  1569. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1570. */
  1571. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1572. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1573. return -EINVAL;
  1574. /* help drivers fail *cleanly* when they need options
  1575. * that aren't supported with their current master
  1576. */
  1577. bad_bits = spi->mode & ~spi->master->mode_bits;
  1578. ugly_bits = bad_bits &
  1579. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  1580. if (ugly_bits) {
  1581. dev_warn(&spi->dev,
  1582. "setup: ignoring unsupported mode bits %x\n",
  1583. ugly_bits);
  1584. spi->mode &= ~ugly_bits;
  1585. bad_bits &= ~ugly_bits;
  1586. }
  1587. if (bad_bits) {
  1588. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1589. bad_bits);
  1590. return -EINVAL;
  1591. }
  1592. if (!spi->bits_per_word)
  1593. spi->bits_per_word = 8;
  1594. if (!spi->max_speed_hz)
  1595. spi->max_speed_hz = spi->master->max_speed_hz;
  1596. if (spi->master->setup)
  1597. status = spi->master->setup(spi);
  1598. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1599. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1600. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1601. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1602. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1603. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1604. spi->bits_per_word, spi->max_speed_hz,
  1605. status);
  1606. return status;
  1607. }
  1608. EXPORT_SYMBOL_GPL(spi_setup);
  1609. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  1610. {
  1611. struct spi_master *master = spi->master;
  1612. struct spi_transfer *xfer;
  1613. int w_size;
  1614. if (list_empty(&message->transfers))
  1615. return -EINVAL;
  1616. /* Half-duplex links include original MicroWire, and ones with
  1617. * only one data pin like SPI_3WIRE (switches direction) or where
  1618. * either MOSI or MISO is missing. They can also be caused by
  1619. * software limitations.
  1620. */
  1621. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1622. || (spi->mode & SPI_3WIRE)) {
  1623. unsigned flags = master->flags;
  1624. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1625. if (xfer->rx_buf && xfer->tx_buf)
  1626. return -EINVAL;
  1627. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1628. return -EINVAL;
  1629. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1630. return -EINVAL;
  1631. }
  1632. }
  1633. /**
  1634. * Set transfer bits_per_word and max speed as spi device default if
  1635. * it is not set for this transfer.
  1636. * Set transfer tx_nbits and rx_nbits as single transfer default
  1637. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1638. */
  1639. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1640. message->frame_length += xfer->len;
  1641. if (!xfer->bits_per_word)
  1642. xfer->bits_per_word = spi->bits_per_word;
  1643. if (!xfer->speed_hz)
  1644. xfer->speed_hz = spi->max_speed_hz;
  1645. if (master->max_speed_hz &&
  1646. xfer->speed_hz > master->max_speed_hz)
  1647. xfer->speed_hz = master->max_speed_hz;
  1648. if (master->bits_per_word_mask) {
  1649. /* Only 32 bits fit in the mask */
  1650. if (xfer->bits_per_word > 32)
  1651. return -EINVAL;
  1652. if (!(master->bits_per_word_mask &
  1653. BIT(xfer->bits_per_word - 1)))
  1654. return -EINVAL;
  1655. }
  1656. /*
  1657. * SPI transfer length should be multiple of SPI word size
  1658. * where SPI word size should be power-of-two multiple
  1659. */
  1660. if (xfer->bits_per_word <= 8)
  1661. w_size = 1;
  1662. else if (xfer->bits_per_word <= 16)
  1663. w_size = 2;
  1664. else
  1665. w_size = 4;
  1666. /* No partial transfers accepted */
  1667. if (xfer->len % w_size)
  1668. return -EINVAL;
  1669. if (xfer->speed_hz && master->min_speed_hz &&
  1670. xfer->speed_hz < master->min_speed_hz)
  1671. return -EINVAL;
  1672. if (xfer->tx_buf && !xfer->tx_nbits)
  1673. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1674. if (xfer->rx_buf && !xfer->rx_nbits)
  1675. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1676. /* check transfer tx/rx_nbits:
  1677. * 1. check the value matches one of single, dual and quad
  1678. * 2. check tx/rx_nbits match the mode in spi_device
  1679. */
  1680. if (xfer->tx_buf) {
  1681. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1682. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1683. xfer->tx_nbits != SPI_NBITS_QUAD)
  1684. return -EINVAL;
  1685. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1686. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1687. return -EINVAL;
  1688. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1689. !(spi->mode & SPI_TX_QUAD))
  1690. return -EINVAL;
  1691. }
  1692. /* check transfer rx_nbits */
  1693. if (xfer->rx_buf) {
  1694. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1695. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1696. xfer->rx_nbits != SPI_NBITS_QUAD)
  1697. return -EINVAL;
  1698. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1699. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1700. return -EINVAL;
  1701. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1702. !(spi->mode & SPI_RX_QUAD))
  1703. return -EINVAL;
  1704. }
  1705. }
  1706. message->status = -EINPROGRESS;
  1707. return 0;
  1708. }
  1709. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1710. {
  1711. struct spi_master *master = spi->master;
  1712. message->spi = spi;
  1713. trace_spi_message_submit(message);
  1714. return master->transfer(spi, message);
  1715. }
  1716. /**
  1717. * spi_async - asynchronous SPI transfer
  1718. * @spi: device with which data will be exchanged
  1719. * @message: describes the data transfers, including completion callback
  1720. * Context: any (irqs may be blocked, etc)
  1721. *
  1722. * This call may be used in_irq and other contexts which can't sleep,
  1723. * as well as from task contexts which can sleep.
  1724. *
  1725. * The completion callback is invoked in a context which can't sleep.
  1726. * Before that invocation, the value of message->status is undefined.
  1727. * When the callback is issued, message->status holds either zero (to
  1728. * indicate complete success) or a negative error code. After that
  1729. * callback returns, the driver which issued the transfer request may
  1730. * deallocate the associated memory; it's no longer in use by any SPI
  1731. * core or controller driver code.
  1732. *
  1733. * Note that although all messages to a spi_device are handled in
  1734. * FIFO order, messages may go to different devices in other orders.
  1735. * Some device might be higher priority, or have various "hard" access
  1736. * time requirements, for example.
  1737. *
  1738. * On detection of any fault during the transfer, processing of
  1739. * the entire message is aborted, and the device is deselected.
  1740. * Until returning from the associated message completion callback,
  1741. * no other spi_message queued to that device will be processed.
  1742. * (This rule applies equally to all the synchronous transfer calls,
  1743. * which are wrappers around this core asynchronous primitive.)
  1744. */
  1745. int spi_async(struct spi_device *spi, struct spi_message *message)
  1746. {
  1747. struct spi_master *master = spi->master;
  1748. int ret;
  1749. unsigned long flags;
  1750. ret = __spi_validate(spi, message);
  1751. if (ret != 0)
  1752. return ret;
  1753. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1754. if (master->bus_lock_flag)
  1755. ret = -EBUSY;
  1756. else
  1757. ret = __spi_async(spi, message);
  1758. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1759. return ret;
  1760. }
  1761. EXPORT_SYMBOL_GPL(spi_async);
  1762. /**
  1763. * spi_async_locked - version of spi_async with exclusive bus usage
  1764. * @spi: device with which data will be exchanged
  1765. * @message: describes the data transfers, including completion callback
  1766. * Context: any (irqs may be blocked, etc)
  1767. *
  1768. * This call may be used in_irq and other contexts which can't sleep,
  1769. * as well as from task contexts which can sleep.
  1770. *
  1771. * The completion callback is invoked in a context which can't sleep.
  1772. * Before that invocation, the value of message->status is undefined.
  1773. * When the callback is issued, message->status holds either zero (to
  1774. * indicate complete success) or a negative error code. After that
  1775. * callback returns, the driver which issued the transfer request may
  1776. * deallocate the associated memory; it's no longer in use by any SPI
  1777. * core or controller driver code.
  1778. *
  1779. * Note that although all messages to a spi_device are handled in
  1780. * FIFO order, messages may go to different devices in other orders.
  1781. * Some device might be higher priority, or have various "hard" access
  1782. * time requirements, for example.
  1783. *
  1784. * On detection of any fault during the transfer, processing of
  1785. * the entire message is aborted, and the device is deselected.
  1786. * Until returning from the associated message completion callback,
  1787. * no other spi_message queued to that device will be processed.
  1788. * (This rule applies equally to all the synchronous transfer calls,
  1789. * which are wrappers around this core asynchronous primitive.)
  1790. */
  1791. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1792. {
  1793. struct spi_master *master = spi->master;
  1794. int ret;
  1795. unsigned long flags;
  1796. ret = __spi_validate(spi, message);
  1797. if (ret != 0)
  1798. return ret;
  1799. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1800. ret = __spi_async(spi, message);
  1801. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1802. return ret;
  1803. }
  1804. EXPORT_SYMBOL_GPL(spi_async_locked);
  1805. /*-------------------------------------------------------------------------*/
  1806. /* Utility methods for SPI master protocol drivers, layered on
  1807. * top of the core. Some other utility methods are defined as
  1808. * inline functions.
  1809. */
  1810. static void spi_complete(void *arg)
  1811. {
  1812. complete(arg);
  1813. }
  1814. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1815. int bus_locked)
  1816. {
  1817. DECLARE_COMPLETION_ONSTACK(done);
  1818. int status;
  1819. struct spi_master *master = spi->master;
  1820. unsigned long flags;
  1821. status = __spi_validate(spi, message);
  1822. if (status != 0)
  1823. return status;
  1824. message->complete = spi_complete;
  1825. message->context = &done;
  1826. message->spi = spi;
  1827. if (!bus_locked)
  1828. mutex_lock(&master->bus_lock_mutex);
  1829. /* If we're not using the legacy transfer method then we will
  1830. * try to transfer in the calling context so special case.
  1831. * This code would be less tricky if we could remove the
  1832. * support for driver implemented message queues.
  1833. */
  1834. if (master->transfer == spi_queued_transfer) {
  1835. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1836. trace_spi_message_submit(message);
  1837. status = __spi_queued_transfer(spi, message, false);
  1838. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1839. } else {
  1840. status = spi_async_locked(spi, message);
  1841. }
  1842. if (!bus_locked)
  1843. mutex_unlock(&master->bus_lock_mutex);
  1844. if (status == 0) {
  1845. /* Push out the messages in the calling context if we
  1846. * can.
  1847. */
  1848. if (master->transfer == spi_queued_transfer)
  1849. __spi_pump_messages(master, false);
  1850. wait_for_completion(&done);
  1851. status = message->status;
  1852. }
  1853. message->context = NULL;
  1854. return status;
  1855. }
  1856. /**
  1857. * spi_sync - blocking/synchronous SPI data transfers
  1858. * @spi: device with which data will be exchanged
  1859. * @message: describes the data transfers
  1860. * Context: can sleep
  1861. *
  1862. * This call may only be used from a context that may sleep. The sleep
  1863. * is non-interruptible, and has no timeout. Low-overhead controller
  1864. * drivers may DMA directly into and out of the message buffers.
  1865. *
  1866. * Note that the SPI device's chip select is active during the message,
  1867. * and then is normally disabled between messages. Drivers for some
  1868. * frequently-used devices may want to minimize costs of selecting a chip,
  1869. * by leaving it selected in anticipation that the next message will go
  1870. * to the same chip. (That may increase power usage.)
  1871. *
  1872. * Also, the caller is guaranteeing that the memory associated with the
  1873. * message will not be freed before this call returns.
  1874. *
  1875. * It returns zero on success, else a negative error code.
  1876. */
  1877. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1878. {
  1879. return __spi_sync(spi, message, 0);
  1880. }
  1881. EXPORT_SYMBOL_GPL(spi_sync);
  1882. /**
  1883. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1884. * @spi: device with which data will be exchanged
  1885. * @message: describes the data transfers
  1886. * Context: can sleep
  1887. *
  1888. * This call may only be used from a context that may sleep. The sleep
  1889. * is non-interruptible, and has no timeout. Low-overhead controller
  1890. * drivers may DMA directly into and out of the message buffers.
  1891. *
  1892. * This call should be used by drivers that require exclusive access to the
  1893. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1894. * be released by a spi_bus_unlock call when the exclusive access is over.
  1895. *
  1896. * It returns zero on success, else a negative error code.
  1897. */
  1898. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1899. {
  1900. return __spi_sync(spi, message, 1);
  1901. }
  1902. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1903. /**
  1904. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1905. * @master: SPI bus master that should be locked for exclusive bus access
  1906. * Context: can sleep
  1907. *
  1908. * This call may only be used from a context that may sleep. The sleep
  1909. * is non-interruptible, and has no timeout.
  1910. *
  1911. * This call should be used by drivers that require exclusive access to the
  1912. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1913. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1914. * and spi_async_locked calls when the SPI bus lock is held.
  1915. *
  1916. * It returns zero on success, else a negative error code.
  1917. */
  1918. int spi_bus_lock(struct spi_master *master)
  1919. {
  1920. unsigned long flags;
  1921. mutex_lock(&master->bus_lock_mutex);
  1922. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1923. master->bus_lock_flag = 1;
  1924. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1925. /* mutex remains locked until spi_bus_unlock is called */
  1926. return 0;
  1927. }
  1928. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1929. /**
  1930. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1931. * @master: SPI bus master that was locked for exclusive bus access
  1932. * Context: can sleep
  1933. *
  1934. * This call may only be used from a context that may sleep. The sleep
  1935. * is non-interruptible, and has no timeout.
  1936. *
  1937. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1938. * call.
  1939. *
  1940. * It returns zero on success, else a negative error code.
  1941. */
  1942. int spi_bus_unlock(struct spi_master *master)
  1943. {
  1944. master->bus_lock_flag = 0;
  1945. mutex_unlock(&master->bus_lock_mutex);
  1946. return 0;
  1947. }
  1948. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1949. /* portable code must never pass more than 32 bytes */
  1950. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1951. static u8 *buf;
  1952. /**
  1953. * spi_write_then_read - SPI synchronous write followed by read
  1954. * @spi: device with which data will be exchanged
  1955. * @txbuf: data to be written (need not be dma-safe)
  1956. * @n_tx: size of txbuf, in bytes
  1957. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1958. * @n_rx: size of rxbuf, in bytes
  1959. * Context: can sleep
  1960. *
  1961. * This performs a half duplex MicroWire style transaction with the
  1962. * device, sending txbuf and then reading rxbuf. The return value
  1963. * is zero for success, else a negative errno status code.
  1964. * This call may only be used from a context that may sleep.
  1965. *
  1966. * Parameters to this routine are always copied using a small buffer;
  1967. * portable code should never use this for more than 32 bytes.
  1968. * Performance-sensitive or bulk transfer code should instead use
  1969. * spi_{async,sync}() calls with dma-safe buffers.
  1970. */
  1971. int spi_write_then_read(struct spi_device *spi,
  1972. const void *txbuf, unsigned n_tx,
  1973. void *rxbuf, unsigned n_rx)
  1974. {
  1975. static DEFINE_MUTEX(lock);
  1976. int status;
  1977. struct spi_message message;
  1978. struct spi_transfer x[2];
  1979. u8 *local_buf;
  1980. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1981. * copying here, (as a pure convenience thing), but we can
  1982. * keep heap costs out of the hot path unless someone else is
  1983. * using the pre-allocated buffer or the transfer is too large.
  1984. */
  1985. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1986. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1987. GFP_KERNEL | GFP_DMA);
  1988. if (!local_buf)
  1989. return -ENOMEM;
  1990. } else {
  1991. local_buf = buf;
  1992. }
  1993. spi_message_init(&message);
  1994. memset(x, 0, sizeof(x));
  1995. if (n_tx) {
  1996. x[0].len = n_tx;
  1997. spi_message_add_tail(&x[0], &message);
  1998. }
  1999. if (n_rx) {
  2000. x[1].len = n_rx;
  2001. spi_message_add_tail(&x[1], &message);
  2002. }
  2003. memcpy(local_buf, txbuf, n_tx);
  2004. x[0].tx_buf = local_buf;
  2005. x[1].rx_buf = local_buf + n_tx;
  2006. /* do the i/o */
  2007. status = spi_sync(spi, &message);
  2008. if (status == 0)
  2009. memcpy(rxbuf, x[1].rx_buf, n_rx);
  2010. if (x[0].tx_buf == buf)
  2011. mutex_unlock(&lock);
  2012. else
  2013. kfree(local_buf);
  2014. return status;
  2015. }
  2016. EXPORT_SYMBOL_GPL(spi_write_then_read);
  2017. /*-------------------------------------------------------------------------*/
  2018. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  2019. static int __spi_of_device_match(struct device *dev, void *data)
  2020. {
  2021. return dev->of_node == data;
  2022. }
  2023. /* must call put_device() when done with returned spi_device device */
  2024. static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
  2025. {
  2026. struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
  2027. __spi_of_device_match);
  2028. return dev ? to_spi_device(dev) : NULL;
  2029. }
  2030. static int __spi_of_master_match(struct device *dev, const void *data)
  2031. {
  2032. return dev->of_node == data;
  2033. }
  2034. /* the spi masters are not using spi_bus, so we find it with another way */
  2035. static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
  2036. {
  2037. struct device *dev;
  2038. dev = class_find_device(&spi_master_class, NULL, node,
  2039. __spi_of_master_match);
  2040. if (!dev)
  2041. return NULL;
  2042. /* reference got in class_find_device */
  2043. return container_of(dev, struct spi_master, dev);
  2044. }
  2045. static int of_spi_notify(struct notifier_block *nb, unsigned long action,
  2046. void *arg)
  2047. {
  2048. struct of_reconfig_data *rd = arg;
  2049. struct spi_master *master;
  2050. struct spi_device *spi;
  2051. switch (of_reconfig_get_state_change(action, arg)) {
  2052. case OF_RECONFIG_CHANGE_ADD:
  2053. master = of_find_spi_master_by_node(rd->dn->parent);
  2054. if (master == NULL)
  2055. return NOTIFY_OK; /* not for us */
  2056. spi = of_register_spi_device(master, rd->dn);
  2057. put_device(&master->dev);
  2058. if (IS_ERR(spi)) {
  2059. pr_err("%s: failed to create for '%s'\n",
  2060. __func__, rd->dn->full_name);
  2061. return notifier_from_errno(PTR_ERR(spi));
  2062. }
  2063. break;
  2064. case OF_RECONFIG_CHANGE_REMOVE:
  2065. /* find our device by node */
  2066. spi = of_find_spi_device_by_node(rd->dn);
  2067. if (spi == NULL)
  2068. return NOTIFY_OK; /* no? not meant for us */
  2069. /* unregister takes one ref away */
  2070. spi_unregister_device(spi);
  2071. /* and put the reference of the find */
  2072. put_device(&spi->dev);
  2073. break;
  2074. }
  2075. return NOTIFY_OK;
  2076. }
  2077. static struct notifier_block spi_of_notifier = {
  2078. .notifier_call = of_spi_notify,
  2079. };
  2080. #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2081. extern struct notifier_block spi_of_notifier;
  2082. #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2083. static int __init spi_init(void)
  2084. {
  2085. int status;
  2086. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  2087. if (!buf) {
  2088. status = -ENOMEM;
  2089. goto err0;
  2090. }
  2091. status = bus_register(&spi_bus_type);
  2092. if (status < 0)
  2093. goto err1;
  2094. status = class_register(&spi_master_class);
  2095. if (status < 0)
  2096. goto err2;
  2097. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  2098. WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
  2099. return 0;
  2100. err2:
  2101. bus_unregister(&spi_bus_type);
  2102. err1:
  2103. kfree(buf);
  2104. buf = NULL;
  2105. err0:
  2106. return status;
  2107. }
  2108. /* board_info is normally registered in arch_initcall(),
  2109. * but even essential drivers wait till later
  2110. *
  2111. * REVISIT only boardinfo really needs static linking. the rest (device and
  2112. * driver registration) _could_ be dynamically linked (modular) ... costs
  2113. * include needing to have boardinfo data structures be much more public.
  2114. */
  2115. postcore_initcall(spi_init);