spi.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  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. if (!n)
  471. return -EINVAL;
  472. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  473. if (!bi)
  474. return -ENOMEM;
  475. for (i = 0; i < n; i++, bi++, info++) {
  476. struct spi_master *master;
  477. memcpy(&bi->board_info, info, sizeof(*info));
  478. mutex_lock(&board_lock);
  479. list_add_tail(&bi->list, &board_list);
  480. list_for_each_entry(master, &spi_master_list, list)
  481. spi_match_master_to_boardinfo(master, &bi->board_info);
  482. mutex_unlock(&board_lock);
  483. }
  484. return 0;
  485. }
  486. /*-------------------------------------------------------------------------*/
  487. static void spi_set_cs(struct spi_device *spi, bool enable)
  488. {
  489. if (spi->mode & SPI_CS_HIGH)
  490. enable = !enable;
  491. if (spi->cs_gpio >= 0)
  492. gpio_set_value(spi->cs_gpio, !enable);
  493. else if (spi->master->set_cs)
  494. spi->master->set_cs(spi, !enable);
  495. }
  496. #ifdef CONFIG_HAS_DMA
  497. static int spi_map_buf(struct spi_master *master, struct device *dev,
  498. struct sg_table *sgt, void *buf, size_t len,
  499. enum dma_data_direction dir)
  500. {
  501. const bool vmalloced_buf = is_vmalloc_addr(buf);
  502. const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len;
  503. const int sgs = DIV_ROUND_UP(len, desc_len);
  504. struct page *vm_page;
  505. void *sg_buf;
  506. size_t min;
  507. int i, ret;
  508. ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
  509. if (ret != 0)
  510. return ret;
  511. for (i = 0; i < sgs; i++) {
  512. min = min_t(size_t, len, desc_len);
  513. if (vmalloced_buf) {
  514. vm_page = vmalloc_to_page(buf);
  515. if (!vm_page) {
  516. sg_free_table(sgt);
  517. return -ENOMEM;
  518. }
  519. sg_buf = page_address(vm_page) +
  520. ((size_t)buf & ~PAGE_MASK);
  521. } else {
  522. sg_buf = buf;
  523. }
  524. sg_set_buf(&sgt->sgl[i], sg_buf, min);
  525. buf += min;
  526. len -= min;
  527. }
  528. ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
  529. if (!ret)
  530. ret = -ENOMEM;
  531. if (ret < 0) {
  532. sg_free_table(sgt);
  533. return ret;
  534. }
  535. sgt->nents = ret;
  536. return 0;
  537. }
  538. static void spi_unmap_buf(struct spi_master *master, struct device *dev,
  539. struct sg_table *sgt, enum dma_data_direction dir)
  540. {
  541. if (sgt->orig_nents) {
  542. dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  543. sg_free_table(sgt);
  544. }
  545. }
  546. static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
  547. {
  548. struct device *tx_dev, *rx_dev;
  549. struct spi_transfer *xfer;
  550. int ret;
  551. if (!master->can_dma)
  552. return 0;
  553. tx_dev = master->dma_tx->device->dev;
  554. rx_dev = master->dma_rx->device->dev;
  555. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  556. if (!master->can_dma(master, msg->spi, xfer))
  557. continue;
  558. if (xfer->tx_buf != NULL) {
  559. ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
  560. (void *)xfer->tx_buf, xfer->len,
  561. DMA_TO_DEVICE);
  562. if (ret != 0)
  563. return ret;
  564. }
  565. if (xfer->rx_buf != NULL) {
  566. ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
  567. xfer->rx_buf, xfer->len,
  568. DMA_FROM_DEVICE);
  569. if (ret != 0) {
  570. spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
  571. DMA_TO_DEVICE);
  572. return ret;
  573. }
  574. }
  575. }
  576. master->cur_msg_mapped = true;
  577. return 0;
  578. }
  579. static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
  580. {
  581. struct spi_transfer *xfer;
  582. struct device *tx_dev, *rx_dev;
  583. if (!master->cur_msg_mapped || !master->can_dma)
  584. return 0;
  585. tx_dev = master->dma_tx->device->dev;
  586. rx_dev = master->dma_rx->device->dev;
  587. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  588. if (!master->can_dma(master, msg->spi, xfer))
  589. continue;
  590. spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
  591. spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
  592. }
  593. return 0;
  594. }
  595. #else /* !CONFIG_HAS_DMA */
  596. static inline int __spi_map_msg(struct spi_master *master,
  597. struct spi_message *msg)
  598. {
  599. return 0;
  600. }
  601. static inline int spi_unmap_msg(struct spi_master *master,
  602. struct spi_message *msg)
  603. {
  604. return 0;
  605. }
  606. #endif /* !CONFIG_HAS_DMA */
  607. static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
  608. {
  609. struct spi_transfer *xfer;
  610. void *tmp;
  611. unsigned int max_tx, max_rx;
  612. if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
  613. max_tx = 0;
  614. max_rx = 0;
  615. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  616. if ((master->flags & SPI_MASTER_MUST_TX) &&
  617. !xfer->tx_buf)
  618. max_tx = max(xfer->len, max_tx);
  619. if ((master->flags & SPI_MASTER_MUST_RX) &&
  620. !xfer->rx_buf)
  621. max_rx = max(xfer->len, max_rx);
  622. }
  623. if (max_tx) {
  624. tmp = krealloc(master->dummy_tx, max_tx,
  625. GFP_KERNEL | GFP_DMA);
  626. if (!tmp)
  627. return -ENOMEM;
  628. master->dummy_tx = tmp;
  629. memset(tmp, 0, max_tx);
  630. }
  631. if (max_rx) {
  632. tmp = krealloc(master->dummy_rx, max_rx,
  633. GFP_KERNEL | GFP_DMA);
  634. if (!tmp)
  635. return -ENOMEM;
  636. master->dummy_rx = tmp;
  637. }
  638. if (max_tx || max_rx) {
  639. list_for_each_entry(xfer, &msg->transfers,
  640. transfer_list) {
  641. if (!xfer->tx_buf)
  642. xfer->tx_buf = master->dummy_tx;
  643. if (!xfer->rx_buf)
  644. xfer->rx_buf = master->dummy_rx;
  645. }
  646. }
  647. }
  648. return __spi_map_msg(master, msg);
  649. }
  650. /*
  651. * spi_transfer_one_message - Default implementation of transfer_one_message()
  652. *
  653. * This is a standard implementation of transfer_one_message() for
  654. * drivers which impelment a transfer_one() operation. It provides
  655. * standard handling of delays and chip select management.
  656. */
  657. static int spi_transfer_one_message(struct spi_master *master,
  658. struct spi_message *msg)
  659. {
  660. struct spi_transfer *xfer;
  661. bool keep_cs = false;
  662. int ret = 0;
  663. int ms = 1;
  664. spi_set_cs(msg->spi, true);
  665. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  666. trace_spi_transfer_start(msg, xfer);
  667. if (xfer->tx_buf || xfer->rx_buf) {
  668. reinit_completion(&master->xfer_completion);
  669. ret = master->transfer_one(master, msg->spi, xfer);
  670. if (ret < 0) {
  671. dev_err(&msg->spi->dev,
  672. "SPI transfer failed: %d\n", ret);
  673. goto out;
  674. }
  675. if (ret > 0) {
  676. ret = 0;
  677. ms = xfer->len * 8 * 1000 / xfer->speed_hz;
  678. ms += ms + 100; /* some tolerance */
  679. ms = wait_for_completion_timeout(&master->xfer_completion,
  680. msecs_to_jiffies(ms));
  681. }
  682. if (ms == 0) {
  683. dev_err(&msg->spi->dev,
  684. "SPI transfer timed out\n");
  685. msg->status = -ETIMEDOUT;
  686. }
  687. } else {
  688. if (xfer->len)
  689. dev_err(&msg->spi->dev,
  690. "Bufferless transfer has length %u\n",
  691. xfer->len);
  692. }
  693. trace_spi_transfer_stop(msg, xfer);
  694. if (msg->status != -EINPROGRESS)
  695. goto out;
  696. if (xfer->delay_usecs)
  697. udelay(xfer->delay_usecs);
  698. if (xfer->cs_change) {
  699. if (list_is_last(&xfer->transfer_list,
  700. &msg->transfers)) {
  701. keep_cs = true;
  702. } else {
  703. spi_set_cs(msg->spi, false);
  704. udelay(10);
  705. spi_set_cs(msg->spi, true);
  706. }
  707. }
  708. msg->actual_length += xfer->len;
  709. }
  710. out:
  711. if (ret != 0 || !keep_cs)
  712. spi_set_cs(msg->spi, false);
  713. if (msg->status == -EINPROGRESS)
  714. msg->status = ret;
  715. spi_finalize_current_message(master);
  716. return ret;
  717. }
  718. /**
  719. * spi_finalize_current_transfer - report completion of a transfer
  720. * @master: the master reporting completion
  721. *
  722. * Called by SPI drivers using the core transfer_one_message()
  723. * implementation to notify it that the current interrupt driven
  724. * transfer has finished and the next one may be scheduled.
  725. */
  726. void spi_finalize_current_transfer(struct spi_master *master)
  727. {
  728. complete(&master->xfer_completion);
  729. }
  730. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  731. /**
  732. * spi_pump_messages - kthread work function which processes spi message queue
  733. * @work: pointer to kthread work struct contained in the master struct
  734. *
  735. * This function checks if there is any spi message in the queue that
  736. * needs processing and if so call out to the driver to initialize hardware
  737. * and transfer each message.
  738. *
  739. */
  740. static void spi_pump_messages(struct kthread_work *work)
  741. {
  742. struct spi_master *master =
  743. container_of(work, struct spi_master, pump_messages);
  744. unsigned long flags;
  745. bool was_busy = false;
  746. int ret;
  747. /* Lock queue and check for queue work */
  748. spin_lock_irqsave(&master->queue_lock, flags);
  749. if (list_empty(&master->queue) || !master->running) {
  750. if (!master->busy) {
  751. spin_unlock_irqrestore(&master->queue_lock, flags);
  752. return;
  753. }
  754. master->busy = false;
  755. spin_unlock_irqrestore(&master->queue_lock, flags);
  756. kfree(master->dummy_rx);
  757. master->dummy_rx = NULL;
  758. kfree(master->dummy_tx);
  759. master->dummy_tx = NULL;
  760. if (master->unprepare_transfer_hardware &&
  761. master->unprepare_transfer_hardware(master))
  762. dev_err(&master->dev,
  763. "failed to unprepare transfer hardware\n");
  764. if (master->auto_runtime_pm) {
  765. pm_runtime_mark_last_busy(master->dev.parent);
  766. pm_runtime_put_autosuspend(master->dev.parent);
  767. }
  768. trace_spi_master_idle(master);
  769. return;
  770. }
  771. /* Make sure we are not already running a message */
  772. if (master->cur_msg) {
  773. spin_unlock_irqrestore(&master->queue_lock, flags);
  774. return;
  775. }
  776. /* Extract head of queue */
  777. master->cur_msg =
  778. list_first_entry(&master->queue, struct spi_message, queue);
  779. list_del_init(&master->cur_msg->queue);
  780. if (master->busy)
  781. was_busy = true;
  782. else
  783. master->busy = true;
  784. spin_unlock_irqrestore(&master->queue_lock, flags);
  785. if (!was_busy && master->auto_runtime_pm) {
  786. ret = pm_runtime_get_sync(master->dev.parent);
  787. if (ret < 0) {
  788. dev_err(&master->dev, "Failed to power device: %d\n",
  789. ret);
  790. return;
  791. }
  792. }
  793. if (!was_busy)
  794. trace_spi_master_busy(master);
  795. if (!was_busy && master->prepare_transfer_hardware) {
  796. ret = master->prepare_transfer_hardware(master);
  797. if (ret) {
  798. dev_err(&master->dev,
  799. "failed to prepare transfer hardware\n");
  800. if (master->auto_runtime_pm)
  801. pm_runtime_put(master->dev.parent);
  802. return;
  803. }
  804. }
  805. trace_spi_message_start(master->cur_msg);
  806. if (master->prepare_message) {
  807. ret = master->prepare_message(master, master->cur_msg);
  808. if (ret) {
  809. dev_err(&master->dev,
  810. "failed to prepare message: %d\n", ret);
  811. master->cur_msg->status = ret;
  812. spi_finalize_current_message(master);
  813. return;
  814. }
  815. master->cur_msg_prepared = true;
  816. }
  817. ret = spi_map_msg(master, master->cur_msg);
  818. if (ret) {
  819. master->cur_msg->status = ret;
  820. spi_finalize_current_message(master);
  821. return;
  822. }
  823. ret = master->transfer_one_message(master, master->cur_msg);
  824. if (ret) {
  825. dev_err(&master->dev,
  826. "failed to transfer one message from queue\n");
  827. return;
  828. }
  829. }
  830. static int spi_init_queue(struct spi_master *master)
  831. {
  832. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  833. INIT_LIST_HEAD(&master->queue);
  834. spin_lock_init(&master->queue_lock);
  835. master->running = false;
  836. master->busy = false;
  837. init_kthread_worker(&master->kworker);
  838. master->kworker_task = kthread_run(kthread_worker_fn,
  839. &master->kworker, "%s",
  840. dev_name(&master->dev));
  841. if (IS_ERR(master->kworker_task)) {
  842. dev_err(&master->dev, "failed to create message pump task\n");
  843. return -ENOMEM;
  844. }
  845. init_kthread_work(&master->pump_messages, spi_pump_messages);
  846. /*
  847. * Master config will indicate if this controller should run the
  848. * message pump with high (realtime) priority to reduce the transfer
  849. * latency on the bus by minimising the delay between a transfer
  850. * request and the scheduling of the message pump thread. Without this
  851. * setting the message pump thread will remain at default priority.
  852. */
  853. if (master->rt) {
  854. dev_info(&master->dev,
  855. "will run message pump with realtime priority\n");
  856. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  857. }
  858. return 0;
  859. }
  860. /**
  861. * spi_get_next_queued_message() - called by driver to check for queued
  862. * messages
  863. * @master: the master to check for queued messages
  864. *
  865. * If there are more messages in the queue, the next message is returned from
  866. * this call.
  867. */
  868. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  869. {
  870. struct spi_message *next;
  871. unsigned long flags;
  872. /* get a pointer to the next message, if any */
  873. spin_lock_irqsave(&master->queue_lock, flags);
  874. next = list_first_entry_or_null(&master->queue, struct spi_message,
  875. queue);
  876. spin_unlock_irqrestore(&master->queue_lock, flags);
  877. return next;
  878. }
  879. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  880. /**
  881. * spi_finalize_current_message() - the current message is complete
  882. * @master: the master to return the message to
  883. *
  884. * Called by the driver to notify the core that the message in the front of the
  885. * queue is complete and can be removed from the queue.
  886. */
  887. void spi_finalize_current_message(struct spi_master *master)
  888. {
  889. struct spi_message *mesg;
  890. unsigned long flags;
  891. int ret;
  892. spin_lock_irqsave(&master->queue_lock, flags);
  893. mesg = master->cur_msg;
  894. master->cur_msg = NULL;
  895. queue_kthread_work(&master->kworker, &master->pump_messages);
  896. spin_unlock_irqrestore(&master->queue_lock, flags);
  897. spi_unmap_msg(master, mesg);
  898. if (master->cur_msg_prepared && master->unprepare_message) {
  899. ret = master->unprepare_message(master, mesg);
  900. if (ret) {
  901. dev_err(&master->dev,
  902. "failed to unprepare message: %d\n", ret);
  903. }
  904. }
  905. master->cur_msg_prepared = false;
  906. mesg->state = NULL;
  907. if (mesg->complete)
  908. mesg->complete(mesg->context);
  909. trace_spi_message_done(mesg);
  910. }
  911. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  912. static int spi_start_queue(struct spi_master *master)
  913. {
  914. unsigned long flags;
  915. spin_lock_irqsave(&master->queue_lock, flags);
  916. if (master->running || master->busy) {
  917. spin_unlock_irqrestore(&master->queue_lock, flags);
  918. return -EBUSY;
  919. }
  920. master->running = true;
  921. master->cur_msg = NULL;
  922. spin_unlock_irqrestore(&master->queue_lock, flags);
  923. queue_kthread_work(&master->kworker, &master->pump_messages);
  924. return 0;
  925. }
  926. static int spi_stop_queue(struct spi_master *master)
  927. {
  928. unsigned long flags;
  929. unsigned limit = 500;
  930. int ret = 0;
  931. spin_lock_irqsave(&master->queue_lock, flags);
  932. /*
  933. * This is a bit lame, but is optimized for the common execution path.
  934. * A wait_queue on the master->busy could be used, but then the common
  935. * execution path (pump_messages) would be required to call wake_up or
  936. * friends on every SPI message. Do this instead.
  937. */
  938. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  939. spin_unlock_irqrestore(&master->queue_lock, flags);
  940. usleep_range(10000, 11000);
  941. spin_lock_irqsave(&master->queue_lock, flags);
  942. }
  943. if (!list_empty(&master->queue) || master->busy)
  944. ret = -EBUSY;
  945. else
  946. master->running = false;
  947. spin_unlock_irqrestore(&master->queue_lock, flags);
  948. if (ret) {
  949. dev_warn(&master->dev,
  950. "could not stop message queue\n");
  951. return ret;
  952. }
  953. return ret;
  954. }
  955. static int spi_destroy_queue(struct spi_master *master)
  956. {
  957. int ret;
  958. ret = spi_stop_queue(master);
  959. /*
  960. * flush_kthread_worker will block until all work is done.
  961. * If the reason that stop_queue timed out is that the work will never
  962. * finish, then it does no good to call flush/stop thread, so
  963. * return anyway.
  964. */
  965. if (ret) {
  966. dev_err(&master->dev, "problem destroying queue\n");
  967. return ret;
  968. }
  969. flush_kthread_worker(&master->kworker);
  970. kthread_stop(master->kworker_task);
  971. return 0;
  972. }
  973. /**
  974. * spi_queued_transfer - transfer function for queued transfers
  975. * @spi: spi device which is requesting transfer
  976. * @msg: spi message which is to handled is queued to driver queue
  977. */
  978. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  979. {
  980. struct spi_master *master = spi->master;
  981. unsigned long flags;
  982. spin_lock_irqsave(&master->queue_lock, flags);
  983. if (!master->running) {
  984. spin_unlock_irqrestore(&master->queue_lock, flags);
  985. return -ESHUTDOWN;
  986. }
  987. msg->actual_length = 0;
  988. msg->status = -EINPROGRESS;
  989. list_add_tail(&msg->queue, &master->queue);
  990. if (!master->busy)
  991. queue_kthread_work(&master->kworker, &master->pump_messages);
  992. spin_unlock_irqrestore(&master->queue_lock, flags);
  993. return 0;
  994. }
  995. static int spi_master_initialize_queue(struct spi_master *master)
  996. {
  997. int ret;
  998. master->transfer = spi_queued_transfer;
  999. if (!master->transfer_one_message)
  1000. master->transfer_one_message = spi_transfer_one_message;
  1001. /* Initialize and start queue */
  1002. ret = spi_init_queue(master);
  1003. if (ret) {
  1004. dev_err(&master->dev, "problem initializing queue\n");
  1005. goto err_init_queue;
  1006. }
  1007. master->queued = true;
  1008. ret = spi_start_queue(master);
  1009. if (ret) {
  1010. dev_err(&master->dev, "problem starting queue\n");
  1011. goto err_start_queue;
  1012. }
  1013. return 0;
  1014. err_start_queue:
  1015. spi_destroy_queue(master);
  1016. err_init_queue:
  1017. return ret;
  1018. }
  1019. /*-------------------------------------------------------------------------*/
  1020. #if defined(CONFIG_OF)
  1021. /**
  1022. * of_register_spi_devices() - Register child devices onto the SPI bus
  1023. * @master: Pointer to spi_master device
  1024. *
  1025. * Registers an spi_device for each child node of master node which has a 'reg'
  1026. * property.
  1027. */
  1028. static void of_register_spi_devices(struct spi_master *master)
  1029. {
  1030. struct spi_device *spi;
  1031. struct device_node *nc;
  1032. int rc;
  1033. u32 value;
  1034. if (!master->dev.of_node)
  1035. return;
  1036. for_each_available_child_of_node(master->dev.of_node, nc) {
  1037. /* Alloc an spi_device */
  1038. spi = spi_alloc_device(master);
  1039. if (!spi) {
  1040. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1041. nc->full_name);
  1042. spi_dev_put(spi);
  1043. continue;
  1044. }
  1045. /* Select device driver */
  1046. if (of_modalias_node(nc, spi->modalias,
  1047. sizeof(spi->modalias)) < 0) {
  1048. dev_err(&master->dev, "cannot find modalias for %s\n",
  1049. nc->full_name);
  1050. spi_dev_put(spi);
  1051. continue;
  1052. }
  1053. /* Device address */
  1054. rc = of_property_read_u32(nc, "reg", &value);
  1055. if (rc) {
  1056. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1057. nc->full_name, rc);
  1058. spi_dev_put(spi);
  1059. continue;
  1060. }
  1061. spi->chip_select = value;
  1062. /* Mode (clock phase/polarity/etc.) */
  1063. if (of_find_property(nc, "spi-cpha", NULL))
  1064. spi->mode |= SPI_CPHA;
  1065. if (of_find_property(nc, "spi-cpol", NULL))
  1066. spi->mode |= SPI_CPOL;
  1067. if (of_find_property(nc, "spi-cs-high", NULL))
  1068. spi->mode |= SPI_CS_HIGH;
  1069. if (of_find_property(nc, "spi-3wire", NULL))
  1070. spi->mode |= SPI_3WIRE;
  1071. if (of_find_property(nc, "spi-lsb-first", NULL))
  1072. spi->mode |= SPI_LSB_FIRST;
  1073. /* Device DUAL/QUAD mode */
  1074. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1075. switch (value) {
  1076. case 1:
  1077. break;
  1078. case 2:
  1079. spi->mode |= SPI_TX_DUAL;
  1080. break;
  1081. case 4:
  1082. spi->mode |= SPI_TX_QUAD;
  1083. break;
  1084. default:
  1085. dev_warn(&master->dev,
  1086. "spi-tx-bus-width %d not supported\n",
  1087. value);
  1088. break;
  1089. }
  1090. }
  1091. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1092. switch (value) {
  1093. case 1:
  1094. break;
  1095. case 2:
  1096. spi->mode |= SPI_RX_DUAL;
  1097. break;
  1098. case 4:
  1099. spi->mode |= SPI_RX_QUAD;
  1100. break;
  1101. default:
  1102. dev_warn(&master->dev,
  1103. "spi-rx-bus-width %d not supported\n",
  1104. value);
  1105. break;
  1106. }
  1107. }
  1108. /* Device speed */
  1109. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1110. if (rc) {
  1111. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1112. nc->full_name, rc);
  1113. spi_dev_put(spi);
  1114. continue;
  1115. }
  1116. spi->max_speed_hz = value;
  1117. /* IRQ */
  1118. spi->irq = irq_of_parse_and_map(nc, 0);
  1119. /* Store a pointer to the node in the device structure */
  1120. of_node_get(nc);
  1121. spi->dev.of_node = nc;
  1122. /* Register the new device */
  1123. request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
  1124. rc = spi_add_device(spi);
  1125. if (rc) {
  1126. dev_err(&master->dev, "spi_device register error %s\n",
  1127. nc->full_name);
  1128. spi_dev_put(spi);
  1129. }
  1130. }
  1131. }
  1132. #else
  1133. static void of_register_spi_devices(struct spi_master *master) { }
  1134. #endif
  1135. #ifdef CONFIG_ACPI
  1136. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1137. {
  1138. struct spi_device *spi = data;
  1139. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1140. struct acpi_resource_spi_serialbus *sb;
  1141. sb = &ares->data.spi_serial_bus;
  1142. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1143. spi->chip_select = sb->device_selection;
  1144. spi->max_speed_hz = sb->connection_speed;
  1145. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1146. spi->mode |= SPI_CPHA;
  1147. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1148. spi->mode |= SPI_CPOL;
  1149. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1150. spi->mode |= SPI_CS_HIGH;
  1151. }
  1152. } else if (spi->irq < 0) {
  1153. struct resource r;
  1154. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1155. spi->irq = r.start;
  1156. }
  1157. /* Always tell the ACPI core to skip this resource */
  1158. return 1;
  1159. }
  1160. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1161. void *data, void **return_value)
  1162. {
  1163. struct spi_master *master = data;
  1164. struct list_head resource_list;
  1165. struct acpi_device *adev;
  1166. struct spi_device *spi;
  1167. int ret;
  1168. if (acpi_bus_get_device(handle, &adev))
  1169. return AE_OK;
  1170. if (acpi_bus_get_status(adev) || !adev->status.present)
  1171. return AE_OK;
  1172. spi = spi_alloc_device(master);
  1173. if (!spi) {
  1174. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1175. dev_name(&adev->dev));
  1176. return AE_NO_MEMORY;
  1177. }
  1178. ACPI_COMPANION_SET(&spi->dev, adev);
  1179. spi->irq = -1;
  1180. INIT_LIST_HEAD(&resource_list);
  1181. ret = acpi_dev_get_resources(adev, &resource_list,
  1182. acpi_spi_add_resource, spi);
  1183. acpi_dev_free_resource_list(&resource_list);
  1184. if (ret < 0 || !spi->max_speed_hz) {
  1185. spi_dev_put(spi);
  1186. return AE_OK;
  1187. }
  1188. adev->power.flags.ignore_parent = true;
  1189. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1190. if (spi_add_device(spi)) {
  1191. adev->power.flags.ignore_parent = false;
  1192. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1193. dev_name(&adev->dev));
  1194. spi_dev_put(spi);
  1195. }
  1196. return AE_OK;
  1197. }
  1198. static void acpi_register_spi_devices(struct spi_master *master)
  1199. {
  1200. acpi_status status;
  1201. acpi_handle handle;
  1202. handle = ACPI_HANDLE(master->dev.parent);
  1203. if (!handle)
  1204. return;
  1205. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1206. acpi_spi_add_device, NULL,
  1207. master, NULL);
  1208. if (ACPI_FAILURE(status))
  1209. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1210. }
  1211. #else
  1212. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1213. #endif /* CONFIG_ACPI */
  1214. static void spi_master_release(struct device *dev)
  1215. {
  1216. struct spi_master *master;
  1217. master = container_of(dev, struct spi_master, dev);
  1218. kfree(master);
  1219. }
  1220. static struct class spi_master_class = {
  1221. .name = "spi_master",
  1222. .owner = THIS_MODULE,
  1223. .dev_release = spi_master_release,
  1224. };
  1225. /**
  1226. * spi_alloc_master - allocate SPI master controller
  1227. * @dev: the controller, possibly using the platform_bus
  1228. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1229. * memory is in the driver_data field of the returned device,
  1230. * accessible with spi_master_get_devdata().
  1231. * Context: can sleep
  1232. *
  1233. * This call is used only by SPI master controller drivers, which are the
  1234. * only ones directly touching chip registers. It's how they allocate
  1235. * an spi_master structure, prior to calling spi_register_master().
  1236. *
  1237. * This must be called from context that can sleep. It returns the SPI
  1238. * master structure on success, else NULL.
  1239. *
  1240. * The caller is responsible for assigning the bus number and initializing
  1241. * the master's methods before calling spi_register_master(); and (after errors
  1242. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  1243. * leak.
  1244. */
  1245. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1246. {
  1247. struct spi_master *master;
  1248. if (!dev)
  1249. return NULL;
  1250. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1251. if (!master)
  1252. return NULL;
  1253. device_initialize(&master->dev);
  1254. master->bus_num = -1;
  1255. master->num_chipselect = 1;
  1256. master->dev.class = &spi_master_class;
  1257. master->dev.parent = get_device(dev);
  1258. spi_master_set_devdata(master, &master[1]);
  1259. return master;
  1260. }
  1261. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1262. #ifdef CONFIG_OF
  1263. static int of_spi_register_master(struct spi_master *master)
  1264. {
  1265. int nb, i, *cs;
  1266. struct device_node *np = master->dev.of_node;
  1267. if (!np)
  1268. return 0;
  1269. nb = of_gpio_named_count(np, "cs-gpios");
  1270. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1271. /* Return error only for an incorrectly formed cs-gpios property */
  1272. if (nb == 0 || nb == -ENOENT)
  1273. return 0;
  1274. else if (nb < 0)
  1275. return nb;
  1276. cs = devm_kzalloc(&master->dev,
  1277. sizeof(int) * master->num_chipselect,
  1278. GFP_KERNEL);
  1279. master->cs_gpios = cs;
  1280. if (!master->cs_gpios)
  1281. return -ENOMEM;
  1282. for (i = 0; i < master->num_chipselect; i++)
  1283. cs[i] = -ENOENT;
  1284. for (i = 0; i < nb; i++)
  1285. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1286. return 0;
  1287. }
  1288. #else
  1289. static int of_spi_register_master(struct spi_master *master)
  1290. {
  1291. return 0;
  1292. }
  1293. #endif
  1294. /**
  1295. * spi_register_master - register SPI master controller
  1296. * @master: initialized master, originally from spi_alloc_master()
  1297. * Context: can sleep
  1298. *
  1299. * SPI master controllers connect to their drivers using some non-SPI bus,
  1300. * such as the platform bus. The final stage of probe() in that code
  1301. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1302. *
  1303. * SPI controllers use board specific (often SOC specific) bus numbers,
  1304. * and board-specific addressing for SPI devices combines those numbers
  1305. * with chip select numbers. Since SPI does not directly support dynamic
  1306. * device identification, boards need configuration tables telling which
  1307. * chip is at which address.
  1308. *
  1309. * This must be called from context that can sleep. It returns zero on
  1310. * success, else a negative error code (dropping the master's refcount).
  1311. * After a successful return, the caller is responsible for calling
  1312. * spi_unregister_master().
  1313. */
  1314. int spi_register_master(struct spi_master *master)
  1315. {
  1316. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1317. struct device *dev = master->dev.parent;
  1318. struct boardinfo *bi;
  1319. int status = -ENODEV;
  1320. int dynamic = 0;
  1321. if (!dev)
  1322. return -ENODEV;
  1323. status = of_spi_register_master(master);
  1324. if (status)
  1325. return status;
  1326. /* even if it's just one always-selected device, there must
  1327. * be at least one chipselect
  1328. */
  1329. if (master->num_chipselect == 0)
  1330. return -EINVAL;
  1331. if ((master->bus_num < 0) && master->dev.of_node)
  1332. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1333. /* convention: dynamically assigned bus IDs count down from the max */
  1334. if (master->bus_num < 0) {
  1335. /* FIXME switch to an IDR based scheme, something like
  1336. * I2C now uses, so we can't run out of "dynamic" IDs
  1337. */
  1338. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1339. dynamic = 1;
  1340. }
  1341. spin_lock_init(&master->bus_lock_spinlock);
  1342. mutex_init(&master->bus_lock_mutex);
  1343. master->bus_lock_flag = 0;
  1344. init_completion(&master->xfer_completion);
  1345. if (!master->max_dma_len)
  1346. master->max_dma_len = INT_MAX;
  1347. /* register the device, then userspace will see it.
  1348. * registration fails if the bus ID is in use.
  1349. */
  1350. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1351. status = device_add(&master->dev);
  1352. if (status < 0)
  1353. goto done;
  1354. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1355. dynamic ? " (dynamic)" : "");
  1356. /* If we're using a queued driver, start the queue */
  1357. if (master->transfer)
  1358. dev_info(dev, "master is unqueued, this is deprecated\n");
  1359. else {
  1360. status = spi_master_initialize_queue(master);
  1361. if (status) {
  1362. device_del(&master->dev);
  1363. goto done;
  1364. }
  1365. }
  1366. mutex_lock(&board_lock);
  1367. list_add_tail(&master->list, &spi_master_list);
  1368. list_for_each_entry(bi, &board_list, list)
  1369. spi_match_master_to_boardinfo(master, &bi->board_info);
  1370. mutex_unlock(&board_lock);
  1371. /* Register devices from the device tree and ACPI */
  1372. of_register_spi_devices(master);
  1373. acpi_register_spi_devices(master);
  1374. done:
  1375. return status;
  1376. }
  1377. EXPORT_SYMBOL_GPL(spi_register_master);
  1378. static void devm_spi_unregister(struct device *dev, void *res)
  1379. {
  1380. spi_unregister_master(*(struct spi_master **)res);
  1381. }
  1382. /**
  1383. * dev_spi_register_master - register managed SPI master controller
  1384. * @dev: device managing SPI master
  1385. * @master: initialized master, originally from spi_alloc_master()
  1386. * Context: can sleep
  1387. *
  1388. * Register a SPI device as with spi_register_master() which will
  1389. * automatically be unregister
  1390. */
  1391. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1392. {
  1393. struct spi_master **ptr;
  1394. int ret;
  1395. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1396. if (!ptr)
  1397. return -ENOMEM;
  1398. ret = spi_register_master(master);
  1399. if (!ret) {
  1400. *ptr = master;
  1401. devres_add(dev, ptr);
  1402. } else {
  1403. devres_free(ptr);
  1404. }
  1405. return ret;
  1406. }
  1407. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1408. static int __unregister(struct device *dev, void *null)
  1409. {
  1410. spi_unregister_device(to_spi_device(dev));
  1411. return 0;
  1412. }
  1413. /**
  1414. * spi_unregister_master - unregister SPI master controller
  1415. * @master: the master being unregistered
  1416. * Context: can sleep
  1417. *
  1418. * This call is used only by SPI master controller drivers, which are the
  1419. * only ones directly touching chip registers.
  1420. *
  1421. * This must be called from context that can sleep.
  1422. */
  1423. void spi_unregister_master(struct spi_master *master)
  1424. {
  1425. int dummy;
  1426. if (master->queued) {
  1427. if (spi_destroy_queue(master))
  1428. dev_err(&master->dev, "queue remove failed\n");
  1429. }
  1430. mutex_lock(&board_lock);
  1431. list_del(&master->list);
  1432. mutex_unlock(&board_lock);
  1433. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1434. device_unregister(&master->dev);
  1435. }
  1436. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1437. int spi_master_suspend(struct spi_master *master)
  1438. {
  1439. int ret;
  1440. /* Basically no-ops for non-queued masters */
  1441. if (!master->queued)
  1442. return 0;
  1443. ret = spi_stop_queue(master);
  1444. if (ret)
  1445. dev_err(&master->dev, "queue stop failed\n");
  1446. return ret;
  1447. }
  1448. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1449. int spi_master_resume(struct spi_master *master)
  1450. {
  1451. int ret;
  1452. if (!master->queued)
  1453. return 0;
  1454. ret = spi_start_queue(master);
  1455. if (ret)
  1456. dev_err(&master->dev, "queue restart failed\n");
  1457. return ret;
  1458. }
  1459. EXPORT_SYMBOL_GPL(spi_master_resume);
  1460. static int __spi_master_match(struct device *dev, const void *data)
  1461. {
  1462. struct spi_master *m;
  1463. const u16 *bus_num = data;
  1464. m = container_of(dev, struct spi_master, dev);
  1465. return m->bus_num == *bus_num;
  1466. }
  1467. /**
  1468. * spi_busnum_to_master - look up master associated with bus_num
  1469. * @bus_num: the master's bus number
  1470. * Context: can sleep
  1471. *
  1472. * This call may be used with devices that are registered after
  1473. * arch init time. It returns a refcounted pointer to the relevant
  1474. * spi_master (which the caller must release), or NULL if there is
  1475. * no such master registered.
  1476. */
  1477. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1478. {
  1479. struct device *dev;
  1480. struct spi_master *master = NULL;
  1481. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1482. __spi_master_match);
  1483. if (dev)
  1484. master = container_of(dev, struct spi_master, dev);
  1485. /* reference got in class_find_device */
  1486. return master;
  1487. }
  1488. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1489. /*-------------------------------------------------------------------------*/
  1490. /* Core methods for SPI master protocol drivers. Some of the
  1491. * other core methods are currently defined as inline functions.
  1492. */
  1493. /**
  1494. * spi_setup - setup SPI mode and clock rate
  1495. * @spi: the device whose settings are being modified
  1496. * Context: can sleep, and no requests are queued to the device
  1497. *
  1498. * SPI protocol drivers may need to update the transfer mode if the
  1499. * device doesn't work with its default. They may likewise need
  1500. * to update clock rates or word sizes from initial values. This function
  1501. * changes those settings, and must be called from a context that can sleep.
  1502. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1503. * effect the next time the device is selected and data is transferred to
  1504. * or from it. When this function returns, the spi device is deselected.
  1505. *
  1506. * Note that this call will fail if the protocol driver specifies an option
  1507. * that the underlying controller or its driver does not support. For
  1508. * example, not all hardware supports wire transfers using nine bit words,
  1509. * LSB-first wire encoding, or active-high chipselects.
  1510. */
  1511. int spi_setup(struct spi_device *spi)
  1512. {
  1513. unsigned bad_bits, ugly_bits;
  1514. int status = 0;
  1515. /* check mode to prevent that DUAL and QUAD set at the same time
  1516. */
  1517. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  1518. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  1519. dev_err(&spi->dev,
  1520. "setup: can not select dual and quad at the same time\n");
  1521. return -EINVAL;
  1522. }
  1523. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  1524. */
  1525. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  1526. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  1527. return -EINVAL;
  1528. /* help drivers fail *cleanly* when they need options
  1529. * that aren't supported with their current master
  1530. */
  1531. bad_bits = spi->mode & ~spi->master->mode_bits;
  1532. ugly_bits = bad_bits &
  1533. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  1534. if (ugly_bits) {
  1535. dev_warn(&spi->dev,
  1536. "setup: ignoring unsupported mode bits %x\n",
  1537. ugly_bits);
  1538. spi->mode &= ~ugly_bits;
  1539. bad_bits &= ~ugly_bits;
  1540. }
  1541. if (bad_bits) {
  1542. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1543. bad_bits);
  1544. return -EINVAL;
  1545. }
  1546. if (!spi->bits_per_word)
  1547. spi->bits_per_word = 8;
  1548. if (!spi->max_speed_hz)
  1549. spi->max_speed_hz = spi->master->max_speed_hz;
  1550. if (spi->master->setup)
  1551. status = spi->master->setup(spi);
  1552. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  1553. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1554. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1555. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1556. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1557. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1558. spi->bits_per_word, spi->max_speed_hz,
  1559. status);
  1560. return status;
  1561. }
  1562. EXPORT_SYMBOL_GPL(spi_setup);
  1563. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  1564. {
  1565. struct spi_master *master = spi->master;
  1566. struct spi_transfer *xfer;
  1567. int w_size;
  1568. if (list_empty(&message->transfers))
  1569. return -EINVAL;
  1570. /* Half-duplex links include original MicroWire, and ones with
  1571. * only one data pin like SPI_3WIRE (switches direction) or where
  1572. * either MOSI or MISO is missing. They can also be caused by
  1573. * software limitations.
  1574. */
  1575. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1576. || (spi->mode & SPI_3WIRE)) {
  1577. unsigned flags = master->flags;
  1578. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1579. if (xfer->rx_buf && xfer->tx_buf)
  1580. return -EINVAL;
  1581. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1582. return -EINVAL;
  1583. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1584. return -EINVAL;
  1585. }
  1586. }
  1587. /**
  1588. * Set transfer bits_per_word and max speed as spi device default if
  1589. * it is not set for this transfer.
  1590. * Set transfer tx_nbits and rx_nbits as single transfer default
  1591. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  1592. */
  1593. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1594. message->frame_length += xfer->len;
  1595. if (!xfer->bits_per_word)
  1596. xfer->bits_per_word = spi->bits_per_word;
  1597. if (!xfer->speed_hz)
  1598. xfer->speed_hz = spi->max_speed_hz;
  1599. if (master->max_speed_hz &&
  1600. xfer->speed_hz > master->max_speed_hz)
  1601. xfer->speed_hz = master->max_speed_hz;
  1602. if (master->bits_per_word_mask) {
  1603. /* Only 32 bits fit in the mask */
  1604. if (xfer->bits_per_word > 32)
  1605. return -EINVAL;
  1606. if (!(master->bits_per_word_mask &
  1607. BIT(xfer->bits_per_word - 1)))
  1608. return -EINVAL;
  1609. }
  1610. /*
  1611. * SPI transfer length should be multiple of SPI word size
  1612. * where SPI word size should be power-of-two multiple
  1613. */
  1614. if (xfer->bits_per_word <= 8)
  1615. w_size = 1;
  1616. else if (xfer->bits_per_word <= 16)
  1617. w_size = 2;
  1618. else
  1619. w_size = 4;
  1620. /* No partial transfers accepted */
  1621. if (xfer->len % w_size)
  1622. return -EINVAL;
  1623. if (xfer->speed_hz && master->min_speed_hz &&
  1624. xfer->speed_hz < master->min_speed_hz)
  1625. return -EINVAL;
  1626. if (xfer->tx_buf && !xfer->tx_nbits)
  1627. xfer->tx_nbits = SPI_NBITS_SINGLE;
  1628. if (xfer->rx_buf && !xfer->rx_nbits)
  1629. xfer->rx_nbits = SPI_NBITS_SINGLE;
  1630. /* check transfer tx/rx_nbits:
  1631. * 1. check the value matches one of single, dual and quad
  1632. * 2. check tx/rx_nbits match the mode in spi_device
  1633. */
  1634. if (xfer->tx_buf) {
  1635. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  1636. xfer->tx_nbits != SPI_NBITS_DUAL &&
  1637. xfer->tx_nbits != SPI_NBITS_QUAD)
  1638. return -EINVAL;
  1639. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  1640. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  1641. return -EINVAL;
  1642. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  1643. !(spi->mode & SPI_TX_QUAD))
  1644. return -EINVAL;
  1645. }
  1646. /* check transfer rx_nbits */
  1647. if (xfer->rx_buf) {
  1648. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  1649. xfer->rx_nbits != SPI_NBITS_DUAL &&
  1650. xfer->rx_nbits != SPI_NBITS_QUAD)
  1651. return -EINVAL;
  1652. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  1653. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  1654. return -EINVAL;
  1655. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  1656. !(spi->mode & SPI_RX_QUAD))
  1657. return -EINVAL;
  1658. }
  1659. }
  1660. message->status = -EINPROGRESS;
  1661. return 0;
  1662. }
  1663. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1664. {
  1665. struct spi_master *master = spi->master;
  1666. message->spi = spi;
  1667. trace_spi_message_submit(message);
  1668. return master->transfer(spi, message);
  1669. }
  1670. /**
  1671. * spi_async - asynchronous SPI transfer
  1672. * @spi: device with which data will be exchanged
  1673. * @message: describes the data transfers, including completion callback
  1674. * Context: any (irqs may be blocked, etc)
  1675. *
  1676. * This call may be used in_irq and other contexts which can't sleep,
  1677. * as well as from task contexts which can sleep.
  1678. *
  1679. * The completion callback is invoked in a context which can't sleep.
  1680. * Before that invocation, the value of message->status is undefined.
  1681. * When the callback is issued, message->status holds either zero (to
  1682. * indicate complete success) or a negative error code. After that
  1683. * callback returns, the driver which issued the transfer request may
  1684. * deallocate the associated memory; it's no longer in use by any SPI
  1685. * core or controller driver code.
  1686. *
  1687. * Note that although all messages to a spi_device are handled in
  1688. * FIFO order, messages may go to different devices in other orders.
  1689. * Some device might be higher priority, or have various "hard" access
  1690. * time requirements, for example.
  1691. *
  1692. * On detection of any fault during the transfer, processing of
  1693. * the entire message is aborted, and the device is deselected.
  1694. * Until returning from the associated message completion callback,
  1695. * no other spi_message queued to that device will be processed.
  1696. * (This rule applies equally to all the synchronous transfer calls,
  1697. * which are wrappers around this core asynchronous primitive.)
  1698. */
  1699. int spi_async(struct spi_device *spi, struct spi_message *message)
  1700. {
  1701. struct spi_master *master = spi->master;
  1702. int ret;
  1703. unsigned long flags;
  1704. ret = __spi_validate(spi, message);
  1705. if (ret != 0)
  1706. return ret;
  1707. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1708. if (master->bus_lock_flag)
  1709. ret = -EBUSY;
  1710. else
  1711. ret = __spi_async(spi, message);
  1712. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1713. return ret;
  1714. }
  1715. EXPORT_SYMBOL_GPL(spi_async);
  1716. /**
  1717. * spi_async_locked - version of spi_async with exclusive bus usage
  1718. * @spi: device with which data will be exchanged
  1719. * @message: describes the data transfers, including completion callback
  1720. * Context: any (irqs may be blocked, etc)
  1721. *
  1722. * This call may be used in_irq and other contexts which can't sleep,
  1723. * as well as from task contexts which can sleep.
  1724. *
  1725. * The completion callback is invoked in a context which can't sleep.
  1726. * Before that invocation, the value of message->status is undefined.
  1727. * When the callback is issued, message->status holds either zero (to
  1728. * indicate complete success) or a negative error code. After that
  1729. * callback returns, the driver which issued the transfer request may
  1730. * deallocate the associated memory; it's no longer in use by any SPI
  1731. * core or controller driver code.
  1732. *
  1733. * Note that although all messages to a spi_device are handled in
  1734. * FIFO order, messages may go to different devices in other orders.
  1735. * Some device might be higher priority, or have various "hard" access
  1736. * time requirements, for example.
  1737. *
  1738. * On detection of any fault during the transfer, processing of
  1739. * the entire message is aborted, and the device is deselected.
  1740. * Until returning from the associated message completion callback,
  1741. * no other spi_message queued to that device will be processed.
  1742. * (This rule applies equally to all the synchronous transfer calls,
  1743. * which are wrappers around this core asynchronous primitive.)
  1744. */
  1745. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1746. {
  1747. struct spi_master *master = spi->master;
  1748. int ret;
  1749. unsigned long flags;
  1750. ret = __spi_validate(spi, message);
  1751. if (ret != 0)
  1752. return ret;
  1753. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1754. ret = __spi_async(spi, message);
  1755. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1756. return ret;
  1757. }
  1758. EXPORT_SYMBOL_GPL(spi_async_locked);
  1759. /*-------------------------------------------------------------------------*/
  1760. /* Utility methods for SPI master protocol drivers, layered on
  1761. * top of the core. Some other utility methods are defined as
  1762. * inline functions.
  1763. */
  1764. static void spi_complete(void *arg)
  1765. {
  1766. complete(arg);
  1767. }
  1768. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1769. int bus_locked)
  1770. {
  1771. DECLARE_COMPLETION_ONSTACK(done);
  1772. int status;
  1773. struct spi_master *master = spi->master;
  1774. message->complete = spi_complete;
  1775. message->context = &done;
  1776. if (!bus_locked)
  1777. mutex_lock(&master->bus_lock_mutex);
  1778. status = spi_async_locked(spi, message);
  1779. if (!bus_locked)
  1780. mutex_unlock(&master->bus_lock_mutex);
  1781. if (status == 0) {
  1782. wait_for_completion(&done);
  1783. status = message->status;
  1784. }
  1785. message->context = NULL;
  1786. return status;
  1787. }
  1788. /**
  1789. * spi_sync - blocking/synchronous SPI data transfers
  1790. * @spi: device with which data will be exchanged
  1791. * @message: describes the data transfers
  1792. * Context: can sleep
  1793. *
  1794. * This call may only be used from a context that may sleep. The sleep
  1795. * is non-interruptible, and has no timeout. Low-overhead controller
  1796. * drivers may DMA directly into and out of the message buffers.
  1797. *
  1798. * Note that the SPI device's chip select is active during the message,
  1799. * and then is normally disabled between messages. Drivers for some
  1800. * frequently-used devices may want to minimize costs of selecting a chip,
  1801. * by leaving it selected in anticipation that the next message will go
  1802. * to the same chip. (That may increase power usage.)
  1803. *
  1804. * Also, the caller is guaranteeing that the memory associated with the
  1805. * message will not be freed before this call returns.
  1806. *
  1807. * It returns zero on success, else a negative error code.
  1808. */
  1809. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1810. {
  1811. return __spi_sync(spi, message, 0);
  1812. }
  1813. EXPORT_SYMBOL_GPL(spi_sync);
  1814. /**
  1815. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1816. * @spi: device with which data will be exchanged
  1817. * @message: describes the data transfers
  1818. * Context: can sleep
  1819. *
  1820. * This call may only be used from a context that may sleep. The sleep
  1821. * is non-interruptible, and has no timeout. Low-overhead controller
  1822. * drivers may DMA directly into and out of the message buffers.
  1823. *
  1824. * This call should be used by drivers that require exclusive access to the
  1825. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1826. * be released by a spi_bus_unlock call when the exclusive access is over.
  1827. *
  1828. * It returns zero on success, else a negative error code.
  1829. */
  1830. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1831. {
  1832. return __spi_sync(spi, message, 1);
  1833. }
  1834. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1835. /**
  1836. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1837. * @master: SPI bus master that should be locked for exclusive bus access
  1838. * Context: can sleep
  1839. *
  1840. * This call may only be used from a context that may sleep. The sleep
  1841. * is non-interruptible, and has no timeout.
  1842. *
  1843. * This call should be used by drivers that require exclusive access to the
  1844. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1845. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1846. * and spi_async_locked calls when the SPI bus lock is held.
  1847. *
  1848. * It returns zero on success, else a negative error code.
  1849. */
  1850. int spi_bus_lock(struct spi_master *master)
  1851. {
  1852. unsigned long flags;
  1853. mutex_lock(&master->bus_lock_mutex);
  1854. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1855. master->bus_lock_flag = 1;
  1856. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1857. /* mutex remains locked until spi_bus_unlock is called */
  1858. return 0;
  1859. }
  1860. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1861. /**
  1862. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1863. * @master: SPI bus master that was locked for exclusive bus access
  1864. * Context: can sleep
  1865. *
  1866. * This call may only be used from a context that may sleep. The sleep
  1867. * is non-interruptible, and has no timeout.
  1868. *
  1869. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1870. * call.
  1871. *
  1872. * It returns zero on success, else a negative error code.
  1873. */
  1874. int spi_bus_unlock(struct spi_master *master)
  1875. {
  1876. master->bus_lock_flag = 0;
  1877. mutex_unlock(&master->bus_lock_mutex);
  1878. return 0;
  1879. }
  1880. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1881. /* portable code must never pass more than 32 bytes */
  1882. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  1883. static u8 *buf;
  1884. /**
  1885. * spi_write_then_read - SPI synchronous write followed by read
  1886. * @spi: device with which data will be exchanged
  1887. * @txbuf: data to be written (need not be dma-safe)
  1888. * @n_tx: size of txbuf, in bytes
  1889. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1890. * @n_rx: size of rxbuf, in bytes
  1891. * Context: can sleep
  1892. *
  1893. * This performs a half duplex MicroWire style transaction with the
  1894. * device, sending txbuf and then reading rxbuf. The return value
  1895. * is zero for success, else a negative errno status code.
  1896. * This call may only be used from a context that may sleep.
  1897. *
  1898. * Parameters to this routine are always copied using a small buffer;
  1899. * portable code should never use this for more than 32 bytes.
  1900. * Performance-sensitive or bulk transfer code should instead use
  1901. * spi_{async,sync}() calls with dma-safe buffers.
  1902. */
  1903. int spi_write_then_read(struct spi_device *spi,
  1904. const void *txbuf, unsigned n_tx,
  1905. void *rxbuf, unsigned n_rx)
  1906. {
  1907. static DEFINE_MUTEX(lock);
  1908. int status;
  1909. struct spi_message message;
  1910. struct spi_transfer x[2];
  1911. u8 *local_buf;
  1912. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1913. * copying here, (as a pure convenience thing), but we can
  1914. * keep heap costs out of the hot path unless someone else is
  1915. * using the pre-allocated buffer or the transfer is too large.
  1916. */
  1917. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1918. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1919. GFP_KERNEL | GFP_DMA);
  1920. if (!local_buf)
  1921. return -ENOMEM;
  1922. } else {
  1923. local_buf = buf;
  1924. }
  1925. spi_message_init(&message);
  1926. memset(x, 0, sizeof(x));
  1927. if (n_tx) {
  1928. x[0].len = n_tx;
  1929. spi_message_add_tail(&x[0], &message);
  1930. }
  1931. if (n_rx) {
  1932. x[1].len = n_rx;
  1933. spi_message_add_tail(&x[1], &message);
  1934. }
  1935. memcpy(local_buf, txbuf, n_tx);
  1936. x[0].tx_buf = local_buf;
  1937. x[1].rx_buf = local_buf + n_tx;
  1938. /* do the i/o */
  1939. status = spi_sync(spi, &message);
  1940. if (status == 0)
  1941. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1942. if (x[0].tx_buf == buf)
  1943. mutex_unlock(&lock);
  1944. else
  1945. kfree(local_buf);
  1946. return status;
  1947. }
  1948. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1949. /*-------------------------------------------------------------------------*/
  1950. static int __init spi_init(void)
  1951. {
  1952. int status;
  1953. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1954. if (!buf) {
  1955. status = -ENOMEM;
  1956. goto err0;
  1957. }
  1958. status = bus_register(&spi_bus_type);
  1959. if (status < 0)
  1960. goto err1;
  1961. status = class_register(&spi_master_class);
  1962. if (status < 0)
  1963. goto err2;
  1964. return 0;
  1965. err2:
  1966. bus_unregister(&spi_bus_type);
  1967. err1:
  1968. kfree(buf);
  1969. buf = NULL;
  1970. err0:
  1971. return status;
  1972. }
  1973. /* board_info is normally registered in arch_initcall(),
  1974. * but even essential drivers wait till later
  1975. *
  1976. * REVISIT only boardinfo really needs static linking. the rest (device and
  1977. * driver registration) _could_ be dynamically linked (modular) ... costs
  1978. * include needing to have boardinfo data structures be much more public.
  1979. */
  1980. postcore_initcall(spi_init);