spi.c 63 KB

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