spi.c 60 KB

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