spi.c 61 KB

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