spi.c 61 KB

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