spi.c 61 KB

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