spi.c 61 KB

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