spi.c 61 KB

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