spi.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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/mutex.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/slab.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/export.h>
  35. #include <linux/sched/rt.h>
  36. #include <linux/delay.h>
  37. #include <linux/kthread.h>
  38. #include <linux/ioport.h>
  39. #include <linux/acpi.h>
  40. static void spidev_release(struct device *dev)
  41. {
  42. struct spi_device *spi = to_spi_device(dev);
  43. /* spi masters may cleanup for released devices */
  44. if (spi->master->cleanup)
  45. spi->master->cleanup(spi);
  46. spi_master_put(spi->master);
  47. kfree(spi);
  48. }
  49. static ssize_t
  50. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  51. {
  52. const struct spi_device *spi = to_spi_device(dev);
  53. return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
  54. }
  55. static struct device_attribute spi_dev_attrs[] = {
  56. __ATTR_RO(modalias),
  57. __ATTR_NULL,
  58. };
  59. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
  60. * and the sysfs version makes coldplug work too.
  61. */
  62. static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
  63. const struct spi_device *sdev)
  64. {
  65. while (id->name[0]) {
  66. if (!strcmp(sdev->modalias, id->name))
  67. return id;
  68. id++;
  69. }
  70. return NULL;
  71. }
  72. const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
  73. {
  74. const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
  75. return spi_match_id(sdrv->id_table, sdev);
  76. }
  77. EXPORT_SYMBOL_GPL(spi_get_device_id);
  78. static int spi_match_device(struct device *dev, struct device_driver *drv)
  79. {
  80. const struct spi_device *spi = to_spi_device(dev);
  81. const struct spi_driver *sdrv = to_spi_driver(drv);
  82. /* Attempt an OF style match */
  83. if (of_driver_match_device(dev, drv))
  84. return 1;
  85. /* Then try ACPI */
  86. if (acpi_driver_match_device(dev, drv))
  87. return 1;
  88. if (sdrv->id_table)
  89. return !!spi_match_id(sdrv->id_table, spi);
  90. return strcmp(spi->modalias, drv->name) == 0;
  91. }
  92. static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
  93. {
  94. const struct spi_device *spi = to_spi_device(dev);
  95. add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
  96. return 0;
  97. }
  98. #ifdef CONFIG_PM_SLEEP
  99. static int spi_legacy_suspend(struct device *dev, pm_message_t message)
  100. {
  101. int value = 0;
  102. struct spi_driver *drv = to_spi_driver(dev->driver);
  103. /* suspend will stop irqs and dma; no more i/o */
  104. if (drv) {
  105. if (drv->suspend)
  106. value = drv->suspend(to_spi_device(dev), message);
  107. else
  108. dev_dbg(dev, "... can't suspend\n");
  109. }
  110. return value;
  111. }
  112. static int spi_legacy_resume(struct device *dev)
  113. {
  114. int value = 0;
  115. struct spi_driver *drv = to_spi_driver(dev->driver);
  116. /* resume may restart the i/o queue */
  117. if (drv) {
  118. if (drv->resume)
  119. value = drv->resume(to_spi_device(dev));
  120. else
  121. dev_dbg(dev, "... can't resume\n");
  122. }
  123. return value;
  124. }
  125. static int spi_pm_suspend(struct device *dev)
  126. {
  127. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  128. if (pm)
  129. return pm_generic_suspend(dev);
  130. else
  131. return spi_legacy_suspend(dev, PMSG_SUSPEND);
  132. }
  133. static int spi_pm_resume(struct device *dev)
  134. {
  135. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  136. if (pm)
  137. return pm_generic_resume(dev);
  138. else
  139. return spi_legacy_resume(dev);
  140. }
  141. static int spi_pm_freeze(struct device *dev)
  142. {
  143. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  144. if (pm)
  145. return pm_generic_freeze(dev);
  146. else
  147. return spi_legacy_suspend(dev, PMSG_FREEZE);
  148. }
  149. static int spi_pm_thaw(struct device *dev)
  150. {
  151. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  152. if (pm)
  153. return pm_generic_thaw(dev);
  154. else
  155. return spi_legacy_resume(dev);
  156. }
  157. static int spi_pm_poweroff(struct device *dev)
  158. {
  159. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  160. if (pm)
  161. return pm_generic_poweroff(dev);
  162. else
  163. return spi_legacy_suspend(dev, PMSG_HIBERNATE);
  164. }
  165. static int spi_pm_restore(struct device *dev)
  166. {
  167. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  168. if (pm)
  169. return pm_generic_restore(dev);
  170. else
  171. return spi_legacy_resume(dev);
  172. }
  173. #else
  174. #define spi_pm_suspend NULL
  175. #define spi_pm_resume NULL
  176. #define spi_pm_freeze NULL
  177. #define spi_pm_thaw NULL
  178. #define spi_pm_poweroff NULL
  179. #define spi_pm_restore NULL
  180. #endif
  181. static const struct dev_pm_ops spi_pm = {
  182. .suspend = spi_pm_suspend,
  183. .resume = spi_pm_resume,
  184. .freeze = spi_pm_freeze,
  185. .thaw = spi_pm_thaw,
  186. .poweroff = spi_pm_poweroff,
  187. .restore = spi_pm_restore,
  188. SET_RUNTIME_PM_OPS(
  189. pm_generic_runtime_suspend,
  190. pm_generic_runtime_resume,
  191. pm_generic_runtime_idle
  192. )
  193. };
  194. struct bus_type spi_bus_type = {
  195. .name = "spi",
  196. .dev_attrs = spi_dev_attrs,
  197. .match = spi_match_device,
  198. .uevent = spi_uevent,
  199. .pm = &spi_pm,
  200. };
  201. EXPORT_SYMBOL_GPL(spi_bus_type);
  202. static int spi_drv_probe(struct device *dev)
  203. {
  204. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  205. return sdrv->probe(to_spi_device(dev));
  206. }
  207. static int spi_drv_remove(struct device *dev)
  208. {
  209. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  210. return sdrv->remove(to_spi_device(dev));
  211. }
  212. static void spi_drv_shutdown(struct device *dev)
  213. {
  214. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  215. sdrv->shutdown(to_spi_device(dev));
  216. }
  217. /**
  218. * spi_register_driver - register a SPI driver
  219. * @sdrv: the driver to register
  220. * Context: can sleep
  221. */
  222. int spi_register_driver(struct spi_driver *sdrv)
  223. {
  224. sdrv->driver.bus = &spi_bus_type;
  225. if (sdrv->probe)
  226. sdrv->driver.probe = spi_drv_probe;
  227. if (sdrv->remove)
  228. sdrv->driver.remove = spi_drv_remove;
  229. if (sdrv->shutdown)
  230. sdrv->driver.shutdown = spi_drv_shutdown;
  231. return driver_register(&sdrv->driver);
  232. }
  233. EXPORT_SYMBOL_GPL(spi_register_driver);
  234. /*-------------------------------------------------------------------------*/
  235. /* SPI devices should normally not be created by SPI device drivers; that
  236. * would make them board-specific. Similarly with SPI master drivers.
  237. * Device registration normally goes into like arch/.../mach.../board-YYY.c
  238. * with other readonly (flashable) information about mainboard devices.
  239. */
  240. struct boardinfo {
  241. struct list_head list;
  242. struct spi_board_info board_info;
  243. };
  244. static LIST_HEAD(board_list);
  245. static LIST_HEAD(spi_master_list);
  246. /*
  247. * Used to protect add/del opertion for board_info list and
  248. * spi_master list, and their matching process
  249. */
  250. static DEFINE_MUTEX(board_lock);
  251. /**
  252. * spi_alloc_device - Allocate a new SPI device
  253. * @master: Controller to which device is connected
  254. * Context: can sleep
  255. *
  256. * Allows a driver to allocate and initialize a spi_device without
  257. * registering it immediately. This allows a driver to directly
  258. * fill the spi_device with device parameters before calling
  259. * spi_add_device() on it.
  260. *
  261. * Caller is responsible to call spi_add_device() on the returned
  262. * spi_device structure to add it to the SPI master. If the caller
  263. * needs to discard the spi_device without adding it, then it should
  264. * call spi_dev_put() on it.
  265. *
  266. * Returns a pointer to the new device, or NULL.
  267. */
  268. struct spi_device *spi_alloc_device(struct spi_master *master)
  269. {
  270. struct spi_device *spi;
  271. struct device *dev = master->dev.parent;
  272. if (!spi_master_get(master))
  273. return NULL;
  274. spi = kzalloc(sizeof *spi, GFP_KERNEL);
  275. if (!spi) {
  276. dev_err(dev, "cannot alloc spi_device\n");
  277. spi_master_put(master);
  278. return NULL;
  279. }
  280. spi->master = master;
  281. spi->dev.parent = &master->dev;
  282. spi->dev.bus = &spi_bus_type;
  283. spi->dev.release = spidev_release;
  284. spi->cs_gpio = -EINVAL;
  285. device_initialize(&spi->dev);
  286. return spi;
  287. }
  288. EXPORT_SYMBOL_GPL(spi_alloc_device);
  289. /**
  290. * spi_add_device - Add spi_device allocated with spi_alloc_device
  291. * @spi: spi_device to register
  292. *
  293. * Companion function to spi_alloc_device. Devices allocated with
  294. * spi_alloc_device can be added onto the spi bus with this function.
  295. *
  296. * Returns 0 on success; negative errno on failure
  297. */
  298. int spi_add_device(struct spi_device *spi)
  299. {
  300. static DEFINE_MUTEX(spi_add_lock);
  301. struct spi_master *master = spi->master;
  302. struct device *dev = master->dev.parent;
  303. struct device *d;
  304. int status;
  305. /* Chipselects are numbered 0..max; validate. */
  306. if (spi->chip_select >= master->num_chipselect) {
  307. dev_err(dev, "cs%d >= max %d\n",
  308. spi->chip_select,
  309. master->num_chipselect);
  310. return -EINVAL;
  311. }
  312. /* Set the bus ID string */
  313. dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
  314. spi->chip_select);
  315. /* We need to make sure there's no other device with this
  316. * chipselect **BEFORE** we call setup(), else we'll trash
  317. * its configuration. Lock against concurrent add() calls.
  318. */
  319. mutex_lock(&spi_add_lock);
  320. d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
  321. if (d != NULL) {
  322. dev_err(dev, "chipselect %d already in use\n",
  323. spi->chip_select);
  324. put_device(d);
  325. status = -EBUSY;
  326. goto done;
  327. }
  328. if (master->cs_gpios)
  329. spi->cs_gpio = master->cs_gpios[spi->chip_select];
  330. /* Drivers may modify this initial i/o setup, but will
  331. * normally rely on the device being setup. Devices
  332. * using SPI_CS_HIGH can't coexist well otherwise...
  333. */
  334. status = spi_setup(spi);
  335. if (status < 0) {
  336. dev_err(dev, "can't setup %s, status %d\n",
  337. dev_name(&spi->dev), status);
  338. goto done;
  339. }
  340. /* Device may be bound to an active driver when this returns */
  341. status = device_add(&spi->dev);
  342. if (status < 0)
  343. dev_err(dev, "can't add %s, status %d\n",
  344. dev_name(&spi->dev), status);
  345. else
  346. dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
  347. done:
  348. mutex_unlock(&spi_add_lock);
  349. return status;
  350. }
  351. EXPORT_SYMBOL_GPL(spi_add_device);
  352. /**
  353. * spi_new_device - instantiate one new SPI device
  354. * @master: Controller to which device is connected
  355. * @chip: Describes the SPI device
  356. * Context: can sleep
  357. *
  358. * On typical mainboards, this is purely internal; and it's not needed
  359. * after board init creates the hard-wired devices. Some development
  360. * platforms may not be able to use spi_register_board_info though, and
  361. * this is exported so that for example a USB or parport based adapter
  362. * driver could add devices (which it would learn about out-of-band).
  363. *
  364. * Returns the new device, or NULL.
  365. */
  366. struct spi_device *spi_new_device(struct spi_master *master,
  367. struct spi_board_info *chip)
  368. {
  369. struct spi_device *proxy;
  370. int status;
  371. /* NOTE: caller did any chip->bus_num checks necessary.
  372. *
  373. * Also, unless we change the return value convention to use
  374. * error-or-pointer (not NULL-or-pointer), troubleshootability
  375. * suggests syslogged diagnostics are best here (ugh).
  376. */
  377. proxy = spi_alloc_device(master);
  378. if (!proxy)
  379. return NULL;
  380. WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
  381. proxy->chip_select = chip->chip_select;
  382. proxy->max_speed_hz = chip->max_speed_hz;
  383. proxy->mode = chip->mode;
  384. proxy->irq = chip->irq;
  385. strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
  386. proxy->dev.platform_data = (void *) chip->platform_data;
  387. proxy->controller_data = chip->controller_data;
  388. proxy->controller_state = NULL;
  389. status = spi_add_device(proxy);
  390. if (status < 0) {
  391. spi_dev_put(proxy);
  392. return NULL;
  393. }
  394. return proxy;
  395. }
  396. EXPORT_SYMBOL_GPL(spi_new_device);
  397. static void spi_match_master_to_boardinfo(struct spi_master *master,
  398. struct spi_board_info *bi)
  399. {
  400. struct spi_device *dev;
  401. if (master->bus_num != bi->bus_num)
  402. return;
  403. dev = spi_new_device(master, bi);
  404. if (!dev)
  405. dev_err(master->dev.parent, "can't create new device for %s\n",
  406. bi->modalias);
  407. }
  408. /**
  409. * spi_register_board_info - register SPI devices for a given board
  410. * @info: array of chip descriptors
  411. * @n: how many descriptors are provided
  412. * Context: can sleep
  413. *
  414. * Board-specific early init code calls this (probably during arch_initcall)
  415. * with segments of the SPI device table. Any device nodes are created later,
  416. * after the relevant parent SPI controller (bus_num) is defined. We keep
  417. * this table of devices forever, so that reloading a controller driver will
  418. * not make Linux forget about these hard-wired devices.
  419. *
  420. * Other code can also call this, e.g. a particular add-on board might provide
  421. * SPI devices through its expansion connector, so code initializing that board
  422. * would naturally declare its SPI devices.
  423. *
  424. * The board info passed can safely be __initdata ... but be careful of
  425. * any embedded pointers (platform_data, etc), they're copied as-is.
  426. */
  427. int spi_register_board_info(struct spi_board_info const *info, unsigned n)
  428. {
  429. struct boardinfo *bi;
  430. int i;
  431. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  432. if (!bi)
  433. return -ENOMEM;
  434. for (i = 0; i < n; i++, bi++, info++) {
  435. struct spi_master *master;
  436. memcpy(&bi->board_info, info, sizeof(*info));
  437. mutex_lock(&board_lock);
  438. list_add_tail(&bi->list, &board_list);
  439. list_for_each_entry(master, &spi_master_list, list)
  440. spi_match_master_to_boardinfo(master, &bi->board_info);
  441. mutex_unlock(&board_lock);
  442. }
  443. return 0;
  444. }
  445. /*-------------------------------------------------------------------------*/
  446. /**
  447. * spi_pump_messages - kthread work function which processes spi message queue
  448. * @work: pointer to kthread work struct contained in the master struct
  449. *
  450. * This function checks if there is any spi message in the queue that
  451. * needs processing and if so call out to the driver to initialize hardware
  452. * and transfer each message.
  453. *
  454. */
  455. static void spi_pump_messages(struct kthread_work *work)
  456. {
  457. struct spi_master *master =
  458. container_of(work, struct spi_master, pump_messages);
  459. unsigned long flags;
  460. bool was_busy = false;
  461. int ret;
  462. /* Lock queue and check for queue work */
  463. spin_lock_irqsave(&master->queue_lock, flags);
  464. if (list_empty(&master->queue) || !master->running) {
  465. if (master->busy && master->unprepare_transfer_hardware) {
  466. ret = master->unprepare_transfer_hardware(master);
  467. if (ret) {
  468. spin_unlock_irqrestore(&master->queue_lock, flags);
  469. dev_err(&master->dev,
  470. "failed to unprepare transfer hardware\n");
  471. return;
  472. }
  473. }
  474. master->busy = false;
  475. spin_unlock_irqrestore(&master->queue_lock, flags);
  476. return;
  477. }
  478. /* Make sure we are not already running a message */
  479. if (master->cur_msg) {
  480. spin_unlock_irqrestore(&master->queue_lock, flags);
  481. return;
  482. }
  483. /* Extract head of queue */
  484. master->cur_msg =
  485. list_entry(master->queue.next, struct spi_message, queue);
  486. list_del_init(&master->cur_msg->queue);
  487. if (master->busy)
  488. was_busy = true;
  489. else
  490. master->busy = true;
  491. spin_unlock_irqrestore(&master->queue_lock, flags);
  492. if (!was_busy && master->prepare_transfer_hardware) {
  493. ret = master->prepare_transfer_hardware(master);
  494. if (ret) {
  495. dev_err(&master->dev,
  496. "failed to prepare transfer hardware\n");
  497. return;
  498. }
  499. }
  500. ret = master->transfer_one_message(master, master->cur_msg);
  501. if (ret) {
  502. dev_err(&master->dev,
  503. "failed to transfer one message from queue\n");
  504. return;
  505. }
  506. }
  507. static int spi_init_queue(struct spi_master *master)
  508. {
  509. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  510. INIT_LIST_HEAD(&master->queue);
  511. spin_lock_init(&master->queue_lock);
  512. master->running = false;
  513. master->busy = false;
  514. init_kthread_worker(&master->kworker);
  515. master->kworker_task = kthread_run(kthread_worker_fn,
  516. &master->kworker,
  517. dev_name(&master->dev));
  518. if (IS_ERR(master->kworker_task)) {
  519. dev_err(&master->dev, "failed to create message pump task\n");
  520. return -ENOMEM;
  521. }
  522. init_kthread_work(&master->pump_messages, spi_pump_messages);
  523. /*
  524. * Master config will indicate if this controller should run the
  525. * message pump with high (realtime) priority to reduce the transfer
  526. * latency on the bus by minimising the delay between a transfer
  527. * request and the scheduling of the message pump thread. Without this
  528. * setting the message pump thread will remain at default priority.
  529. */
  530. if (master->rt) {
  531. dev_info(&master->dev,
  532. "will run message pump with realtime priority\n");
  533. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  534. }
  535. return 0;
  536. }
  537. /**
  538. * spi_get_next_queued_message() - called by driver to check for queued
  539. * messages
  540. * @master: the master to check for queued messages
  541. *
  542. * If there are more messages in the queue, the next message is returned from
  543. * this call.
  544. */
  545. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  546. {
  547. struct spi_message *next;
  548. unsigned long flags;
  549. /* get a pointer to the next message, if any */
  550. spin_lock_irqsave(&master->queue_lock, flags);
  551. if (list_empty(&master->queue))
  552. next = NULL;
  553. else
  554. next = list_entry(master->queue.next,
  555. struct spi_message, queue);
  556. spin_unlock_irqrestore(&master->queue_lock, flags);
  557. return next;
  558. }
  559. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  560. /**
  561. * spi_finalize_current_message() - the current message is complete
  562. * @master: the master to return the message to
  563. *
  564. * Called by the driver to notify the core that the message in the front of the
  565. * queue is complete and can be removed from the queue.
  566. */
  567. void spi_finalize_current_message(struct spi_master *master)
  568. {
  569. struct spi_message *mesg;
  570. unsigned long flags;
  571. spin_lock_irqsave(&master->queue_lock, flags);
  572. mesg = master->cur_msg;
  573. master->cur_msg = NULL;
  574. queue_kthread_work(&master->kworker, &master->pump_messages);
  575. spin_unlock_irqrestore(&master->queue_lock, flags);
  576. mesg->state = NULL;
  577. if (mesg->complete)
  578. mesg->complete(mesg->context);
  579. }
  580. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  581. static int spi_start_queue(struct spi_master *master)
  582. {
  583. unsigned long flags;
  584. spin_lock_irqsave(&master->queue_lock, flags);
  585. if (master->running || master->busy) {
  586. spin_unlock_irqrestore(&master->queue_lock, flags);
  587. return -EBUSY;
  588. }
  589. master->running = true;
  590. master->cur_msg = NULL;
  591. spin_unlock_irqrestore(&master->queue_lock, flags);
  592. queue_kthread_work(&master->kworker, &master->pump_messages);
  593. return 0;
  594. }
  595. static int spi_stop_queue(struct spi_master *master)
  596. {
  597. unsigned long flags;
  598. unsigned limit = 500;
  599. int ret = 0;
  600. spin_lock_irqsave(&master->queue_lock, flags);
  601. /*
  602. * This is a bit lame, but is optimized for the common execution path.
  603. * A wait_queue on the master->busy could be used, but then the common
  604. * execution path (pump_messages) would be required to call wake_up or
  605. * friends on every SPI message. Do this instead.
  606. */
  607. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  608. spin_unlock_irqrestore(&master->queue_lock, flags);
  609. msleep(10);
  610. spin_lock_irqsave(&master->queue_lock, flags);
  611. }
  612. if (!list_empty(&master->queue) || master->busy)
  613. ret = -EBUSY;
  614. else
  615. master->running = false;
  616. spin_unlock_irqrestore(&master->queue_lock, flags);
  617. if (ret) {
  618. dev_warn(&master->dev,
  619. "could not stop message queue\n");
  620. return ret;
  621. }
  622. return ret;
  623. }
  624. static int spi_destroy_queue(struct spi_master *master)
  625. {
  626. int ret;
  627. ret = spi_stop_queue(master);
  628. /*
  629. * flush_kthread_worker will block until all work is done.
  630. * If the reason that stop_queue timed out is that the work will never
  631. * finish, then it does no good to call flush/stop thread, so
  632. * return anyway.
  633. */
  634. if (ret) {
  635. dev_err(&master->dev, "problem destroying queue\n");
  636. return ret;
  637. }
  638. flush_kthread_worker(&master->kworker);
  639. kthread_stop(master->kworker_task);
  640. return 0;
  641. }
  642. /**
  643. * spi_queued_transfer - transfer function for queued transfers
  644. * @spi: spi device which is requesting transfer
  645. * @msg: spi message which is to handled is queued to driver queue
  646. */
  647. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  648. {
  649. struct spi_master *master = spi->master;
  650. unsigned long flags;
  651. spin_lock_irqsave(&master->queue_lock, flags);
  652. if (!master->running) {
  653. spin_unlock_irqrestore(&master->queue_lock, flags);
  654. return -ESHUTDOWN;
  655. }
  656. msg->actual_length = 0;
  657. msg->status = -EINPROGRESS;
  658. list_add_tail(&msg->queue, &master->queue);
  659. if (master->running && !master->busy)
  660. queue_kthread_work(&master->kworker, &master->pump_messages);
  661. spin_unlock_irqrestore(&master->queue_lock, flags);
  662. return 0;
  663. }
  664. static int spi_master_initialize_queue(struct spi_master *master)
  665. {
  666. int ret;
  667. master->queued = true;
  668. master->transfer = spi_queued_transfer;
  669. /* Initialize and start queue */
  670. ret = spi_init_queue(master);
  671. if (ret) {
  672. dev_err(&master->dev, "problem initializing queue\n");
  673. goto err_init_queue;
  674. }
  675. ret = spi_start_queue(master);
  676. if (ret) {
  677. dev_err(&master->dev, "problem starting queue\n");
  678. goto err_start_queue;
  679. }
  680. return 0;
  681. err_start_queue:
  682. err_init_queue:
  683. spi_destroy_queue(master);
  684. return ret;
  685. }
  686. /*-------------------------------------------------------------------------*/
  687. #if defined(CONFIG_OF)
  688. /**
  689. * of_register_spi_devices() - Register child devices onto the SPI bus
  690. * @master: Pointer to spi_master device
  691. *
  692. * Registers an spi_device for each child node of master node which has a 'reg'
  693. * property.
  694. */
  695. static void of_register_spi_devices(struct spi_master *master)
  696. {
  697. struct spi_device *spi;
  698. struct device_node *nc;
  699. const __be32 *prop;
  700. char modalias[SPI_NAME_SIZE + 4];
  701. int rc;
  702. int len;
  703. if (!master->dev.of_node)
  704. return;
  705. for_each_available_child_of_node(master->dev.of_node, nc) {
  706. /* Alloc an spi_device */
  707. spi = spi_alloc_device(master);
  708. if (!spi) {
  709. dev_err(&master->dev, "spi_device alloc error for %s\n",
  710. nc->full_name);
  711. spi_dev_put(spi);
  712. continue;
  713. }
  714. /* Select device driver */
  715. if (of_modalias_node(nc, spi->modalias,
  716. sizeof(spi->modalias)) < 0) {
  717. dev_err(&master->dev, "cannot find modalias for %s\n",
  718. nc->full_name);
  719. spi_dev_put(spi);
  720. continue;
  721. }
  722. /* Device address */
  723. prop = of_get_property(nc, "reg", &len);
  724. if (!prop || len < sizeof(*prop)) {
  725. dev_err(&master->dev, "%s has no 'reg' property\n",
  726. nc->full_name);
  727. spi_dev_put(spi);
  728. continue;
  729. }
  730. spi->chip_select = be32_to_cpup(prop);
  731. /* Mode (clock phase/polarity/etc.) */
  732. if (of_find_property(nc, "spi-cpha", NULL))
  733. spi->mode |= SPI_CPHA;
  734. if (of_find_property(nc, "spi-cpol", NULL))
  735. spi->mode |= SPI_CPOL;
  736. if (of_find_property(nc, "spi-cs-high", NULL))
  737. spi->mode |= SPI_CS_HIGH;
  738. if (of_find_property(nc, "spi-3wire", NULL))
  739. spi->mode |= SPI_3WIRE;
  740. /* Device speed */
  741. prop = of_get_property(nc, "spi-max-frequency", &len);
  742. if (!prop || len < sizeof(*prop)) {
  743. dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
  744. nc->full_name);
  745. spi_dev_put(spi);
  746. continue;
  747. }
  748. spi->max_speed_hz = be32_to_cpup(prop);
  749. /* IRQ */
  750. spi->irq = irq_of_parse_and_map(nc, 0);
  751. /* Store a pointer to the node in the device structure */
  752. of_node_get(nc);
  753. spi->dev.of_node = nc;
  754. /* Register the new device */
  755. snprintf(modalias, sizeof(modalias), "%s%s", SPI_MODULE_PREFIX,
  756. spi->modalias);
  757. request_module(modalias);
  758. rc = spi_add_device(spi);
  759. if (rc) {
  760. dev_err(&master->dev, "spi_device register error %s\n",
  761. nc->full_name);
  762. spi_dev_put(spi);
  763. }
  764. }
  765. }
  766. #else
  767. static void of_register_spi_devices(struct spi_master *master) { }
  768. #endif
  769. #ifdef CONFIG_ACPI
  770. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  771. {
  772. struct spi_device *spi = data;
  773. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  774. struct acpi_resource_spi_serialbus *sb;
  775. sb = &ares->data.spi_serial_bus;
  776. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  777. spi->chip_select = sb->device_selection;
  778. spi->max_speed_hz = sb->connection_speed;
  779. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  780. spi->mode |= SPI_CPHA;
  781. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  782. spi->mode |= SPI_CPOL;
  783. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  784. spi->mode |= SPI_CS_HIGH;
  785. }
  786. } else if (spi->irq < 0) {
  787. struct resource r;
  788. if (acpi_dev_resource_interrupt(ares, 0, &r))
  789. spi->irq = r.start;
  790. }
  791. /* Always tell the ACPI core to skip this resource */
  792. return 1;
  793. }
  794. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  795. void *data, void **return_value)
  796. {
  797. struct spi_master *master = data;
  798. struct list_head resource_list;
  799. struct acpi_device *adev;
  800. struct spi_device *spi;
  801. int ret;
  802. if (acpi_bus_get_device(handle, &adev))
  803. return AE_OK;
  804. if (acpi_bus_get_status(adev) || !adev->status.present)
  805. return AE_OK;
  806. spi = spi_alloc_device(master);
  807. if (!spi) {
  808. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  809. dev_name(&adev->dev));
  810. return AE_NO_MEMORY;
  811. }
  812. ACPI_HANDLE_SET(&spi->dev, handle);
  813. spi->irq = -1;
  814. INIT_LIST_HEAD(&resource_list);
  815. ret = acpi_dev_get_resources(adev, &resource_list,
  816. acpi_spi_add_resource, spi);
  817. acpi_dev_free_resource_list(&resource_list);
  818. if (ret < 0 || !spi->max_speed_hz) {
  819. spi_dev_put(spi);
  820. return AE_OK;
  821. }
  822. strlcpy(spi->modalias, dev_name(&adev->dev), sizeof(spi->modalias));
  823. if (spi_add_device(spi)) {
  824. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  825. dev_name(&adev->dev));
  826. spi_dev_put(spi);
  827. }
  828. return AE_OK;
  829. }
  830. static void acpi_register_spi_devices(struct spi_master *master)
  831. {
  832. acpi_status status;
  833. acpi_handle handle;
  834. handle = ACPI_HANDLE(&master->dev);
  835. if (!handle)
  836. return;
  837. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  838. acpi_spi_add_device, NULL,
  839. master, NULL);
  840. if (ACPI_FAILURE(status))
  841. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  842. }
  843. #else
  844. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  845. #endif /* CONFIG_ACPI */
  846. static void spi_master_release(struct device *dev)
  847. {
  848. struct spi_master *master;
  849. master = container_of(dev, struct spi_master, dev);
  850. kfree(master);
  851. }
  852. static struct class spi_master_class = {
  853. .name = "spi_master",
  854. .owner = THIS_MODULE,
  855. .dev_release = spi_master_release,
  856. };
  857. /**
  858. * spi_alloc_master - allocate SPI master controller
  859. * @dev: the controller, possibly using the platform_bus
  860. * @size: how much zeroed driver-private data to allocate; the pointer to this
  861. * memory is in the driver_data field of the returned device,
  862. * accessible with spi_master_get_devdata().
  863. * Context: can sleep
  864. *
  865. * This call is used only by SPI master controller drivers, which are the
  866. * only ones directly touching chip registers. It's how they allocate
  867. * an spi_master structure, prior to calling spi_register_master().
  868. *
  869. * This must be called from context that can sleep. It returns the SPI
  870. * master structure on success, else NULL.
  871. *
  872. * The caller is responsible for assigning the bus number and initializing
  873. * the master's methods before calling spi_register_master(); and (after errors
  874. * adding the device) calling spi_master_put() and kfree() to prevent a memory
  875. * leak.
  876. */
  877. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  878. {
  879. struct spi_master *master;
  880. if (!dev)
  881. return NULL;
  882. master = kzalloc(size + sizeof *master, GFP_KERNEL);
  883. if (!master)
  884. return NULL;
  885. device_initialize(&master->dev);
  886. master->bus_num = -1;
  887. master->num_chipselect = 1;
  888. master->dev.class = &spi_master_class;
  889. master->dev.parent = get_device(dev);
  890. spi_master_set_devdata(master, &master[1]);
  891. return master;
  892. }
  893. EXPORT_SYMBOL_GPL(spi_alloc_master);
  894. #ifdef CONFIG_OF
  895. static int of_spi_register_master(struct spi_master *master)
  896. {
  897. int nb, i, *cs;
  898. struct device_node *np = master->dev.of_node;
  899. if (!np)
  900. return 0;
  901. nb = of_gpio_named_count(np, "cs-gpios");
  902. master->num_chipselect = max(nb, (int)master->num_chipselect);
  903. if (nb < 1)
  904. return 0;
  905. cs = devm_kzalloc(&master->dev,
  906. sizeof(int) * master->num_chipselect,
  907. GFP_KERNEL);
  908. master->cs_gpios = cs;
  909. if (!master->cs_gpios)
  910. return -ENOMEM;
  911. for (i = 0; i < master->num_chipselect; i++)
  912. cs[i] = -EINVAL;
  913. for (i = 0; i < nb; i++)
  914. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  915. return 0;
  916. }
  917. #else
  918. static int of_spi_register_master(struct spi_master *master)
  919. {
  920. return 0;
  921. }
  922. #endif
  923. /**
  924. * spi_register_master - register SPI master controller
  925. * @master: initialized master, originally from spi_alloc_master()
  926. * Context: can sleep
  927. *
  928. * SPI master controllers connect to their drivers using some non-SPI bus,
  929. * such as the platform bus. The final stage of probe() in that code
  930. * includes calling spi_register_master() to hook up to this SPI bus glue.
  931. *
  932. * SPI controllers use board specific (often SOC specific) bus numbers,
  933. * and board-specific addressing for SPI devices combines those numbers
  934. * with chip select numbers. Since SPI does not directly support dynamic
  935. * device identification, boards need configuration tables telling which
  936. * chip is at which address.
  937. *
  938. * This must be called from context that can sleep. It returns zero on
  939. * success, else a negative error code (dropping the master's refcount).
  940. * After a successful return, the caller is responsible for calling
  941. * spi_unregister_master().
  942. */
  943. int spi_register_master(struct spi_master *master)
  944. {
  945. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  946. struct device *dev = master->dev.parent;
  947. struct boardinfo *bi;
  948. int status = -ENODEV;
  949. int dynamic = 0;
  950. if (!dev)
  951. return -ENODEV;
  952. status = of_spi_register_master(master);
  953. if (status)
  954. return status;
  955. /* even if it's just one always-selected device, there must
  956. * be at least one chipselect
  957. */
  958. if (master->num_chipselect == 0)
  959. return -EINVAL;
  960. if ((master->bus_num < 0) && master->dev.of_node)
  961. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  962. /* convention: dynamically assigned bus IDs count down from the max */
  963. if (master->bus_num < 0) {
  964. /* FIXME switch to an IDR based scheme, something like
  965. * I2C now uses, so we can't run out of "dynamic" IDs
  966. */
  967. master->bus_num = atomic_dec_return(&dyn_bus_id);
  968. dynamic = 1;
  969. }
  970. spin_lock_init(&master->bus_lock_spinlock);
  971. mutex_init(&master->bus_lock_mutex);
  972. master->bus_lock_flag = 0;
  973. /* register the device, then userspace will see it.
  974. * registration fails if the bus ID is in use.
  975. */
  976. dev_set_name(&master->dev, "spi%u", master->bus_num);
  977. status = device_add(&master->dev);
  978. if (status < 0)
  979. goto done;
  980. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  981. dynamic ? " (dynamic)" : "");
  982. /* If we're using a queued driver, start the queue */
  983. if (master->transfer)
  984. dev_info(dev, "master is unqueued, this is deprecated\n");
  985. else {
  986. status = spi_master_initialize_queue(master);
  987. if (status) {
  988. device_unregister(&master->dev);
  989. goto done;
  990. }
  991. }
  992. mutex_lock(&board_lock);
  993. list_add_tail(&master->list, &spi_master_list);
  994. list_for_each_entry(bi, &board_list, list)
  995. spi_match_master_to_boardinfo(master, &bi->board_info);
  996. mutex_unlock(&board_lock);
  997. /* Register devices from the device tree and ACPI */
  998. of_register_spi_devices(master);
  999. acpi_register_spi_devices(master);
  1000. done:
  1001. return status;
  1002. }
  1003. EXPORT_SYMBOL_GPL(spi_register_master);
  1004. static int __unregister(struct device *dev, void *null)
  1005. {
  1006. spi_unregister_device(to_spi_device(dev));
  1007. return 0;
  1008. }
  1009. /**
  1010. * spi_unregister_master - unregister SPI master controller
  1011. * @master: the master being unregistered
  1012. * Context: can sleep
  1013. *
  1014. * This call is used only by SPI master controller drivers, which are the
  1015. * only ones directly touching chip registers.
  1016. *
  1017. * This must be called from context that can sleep.
  1018. */
  1019. void spi_unregister_master(struct spi_master *master)
  1020. {
  1021. int dummy;
  1022. if (master->queued) {
  1023. if (spi_destroy_queue(master))
  1024. dev_err(&master->dev, "queue remove failed\n");
  1025. }
  1026. mutex_lock(&board_lock);
  1027. list_del(&master->list);
  1028. mutex_unlock(&board_lock);
  1029. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1030. device_unregister(&master->dev);
  1031. }
  1032. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1033. int spi_master_suspend(struct spi_master *master)
  1034. {
  1035. int ret;
  1036. /* Basically no-ops for non-queued masters */
  1037. if (!master->queued)
  1038. return 0;
  1039. ret = spi_stop_queue(master);
  1040. if (ret)
  1041. dev_err(&master->dev, "queue stop failed\n");
  1042. return ret;
  1043. }
  1044. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1045. int spi_master_resume(struct spi_master *master)
  1046. {
  1047. int ret;
  1048. if (!master->queued)
  1049. return 0;
  1050. ret = spi_start_queue(master);
  1051. if (ret)
  1052. dev_err(&master->dev, "queue restart failed\n");
  1053. return ret;
  1054. }
  1055. EXPORT_SYMBOL_GPL(spi_master_resume);
  1056. static int __spi_master_match(struct device *dev, const void *data)
  1057. {
  1058. struct spi_master *m;
  1059. const u16 *bus_num = data;
  1060. m = container_of(dev, struct spi_master, dev);
  1061. return m->bus_num == *bus_num;
  1062. }
  1063. /**
  1064. * spi_busnum_to_master - look up master associated with bus_num
  1065. * @bus_num: the master's bus number
  1066. * Context: can sleep
  1067. *
  1068. * This call may be used with devices that are registered after
  1069. * arch init time. It returns a refcounted pointer to the relevant
  1070. * spi_master (which the caller must release), or NULL if there is
  1071. * no such master registered.
  1072. */
  1073. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1074. {
  1075. struct device *dev;
  1076. struct spi_master *master = NULL;
  1077. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1078. __spi_master_match);
  1079. if (dev)
  1080. master = container_of(dev, struct spi_master, dev);
  1081. /* reference got in class_find_device */
  1082. return master;
  1083. }
  1084. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1085. /*-------------------------------------------------------------------------*/
  1086. /* Core methods for SPI master protocol drivers. Some of the
  1087. * other core methods are currently defined as inline functions.
  1088. */
  1089. /**
  1090. * spi_setup - setup SPI mode and clock rate
  1091. * @spi: the device whose settings are being modified
  1092. * Context: can sleep, and no requests are queued to the device
  1093. *
  1094. * SPI protocol drivers may need to update the transfer mode if the
  1095. * device doesn't work with its default. They may likewise need
  1096. * to update clock rates or word sizes from initial values. This function
  1097. * changes those settings, and must be called from a context that can sleep.
  1098. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  1099. * effect the next time the device is selected and data is transferred to
  1100. * or from it. When this function returns, the spi device is deselected.
  1101. *
  1102. * Note that this call will fail if the protocol driver specifies an option
  1103. * that the underlying controller or its driver does not support. For
  1104. * example, not all hardware supports wire transfers using nine bit words,
  1105. * LSB-first wire encoding, or active-high chipselects.
  1106. */
  1107. int spi_setup(struct spi_device *spi)
  1108. {
  1109. unsigned bad_bits;
  1110. int status = 0;
  1111. /* help drivers fail *cleanly* when they need options
  1112. * that aren't supported with their current master
  1113. */
  1114. bad_bits = spi->mode & ~spi->master->mode_bits;
  1115. if (bad_bits) {
  1116. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  1117. bad_bits);
  1118. return -EINVAL;
  1119. }
  1120. if (!spi->bits_per_word)
  1121. spi->bits_per_word = 8;
  1122. if (spi->master->setup)
  1123. status = spi->master->setup(spi);
  1124. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
  1125. "%u bits/w, %u Hz max --> %d\n",
  1126. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  1127. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  1128. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  1129. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  1130. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  1131. spi->bits_per_word, spi->max_speed_hz,
  1132. status);
  1133. return status;
  1134. }
  1135. EXPORT_SYMBOL_GPL(spi_setup);
  1136. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  1137. {
  1138. struct spi_master *master = spi->master;
  1139. struct spi_transfer *xfer;
  1140. /* Half-duplex links include original MicroWire, and ones with
  1141. * only one data pin like SPI_3WIRE (switches direction) or where
  1142. * either MOSI or MISO is missing. They can also be caused by
  1143. * software limitations.
  1144. */
  1145. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  1146. || (spi->mode & SPI_3WIRE)) {
  1147. unsigned flags = master->flags;
  1148. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1149. if (xfer->rx_buf && xfer->tx_buf)
  1150. return -EINVAL;
  1151. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  1152. return -EINVAL;
  1153. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  1154. return -EINVAL;
  1155. }
  1156. }
  1157. /**
  1158. * Set transfer bits_per_word and max speed as spi device default if
  1159. * it is not set for this transfer.
  1160. */
  1161. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  1162. if (!xfer->bits_per_word)
  1163. xfer->bits_per_word = spi->bits_per_word;
  1164. if (!xfer->speed_hz)
  1165. xfer->speed_hz = spi->max_speed_hz;
  1166. }
  1167. message->spi = spi;
  1168. message->status = -EINPROGRESS;
  1169. return master->transfer(spi, message);
  1170. }
  1171. /**
  1172. * spi_async - asynchronous SPI transfer
  1173. * @spi: device with which data will be exchanged
  1174. * @message: describes the data transfers, including completion callback
  1175. * Context: any (irqs may be blocked, etc)
  1176. *
  1177. * This call may be used in_irq and other contexts which can't sleep,
  1178. * as well as from task contexts which can sleep.
  1179. *
  1180. * The completion callback is invoked in a context which can't sleep.
  1181. * Before that invocation, the value of message->status is undefined.
  1182. * When the callback is issued, message->status holds either zero (to
  1183. * indicate complete success) or a negative error code. After that
  1184. * callback returns, the driver which issued the transfer request may
  1185. * deallocate the associated memory; it's no longer in use by any SPI
  1186. * core or controller driver code.
  1187. *
  1188. * Note that although all messages to a spi_device are handled in
  1189. * FIFO order, messages may go to different devices in other orders.
  1190. * Some device might be higher priority, or have various "hard" access
  1191. * time requirements, for example.
  1192. *
  1193. * On detection of any fault during the transfer, processing of
  1194. * the entire message is aborted, and the device is deselected.
  1195. * Until returning from the associated message completion callback,
  1196. * no other spi_message queued to that device will be processed.
  1197. * (This rule applies equally to all the synchronous transfer calls,
  1198. * which are wrappers around this core asynchronous primitive.)
  1199. */
  1200. int spi_async(struct spi_device *spi, struct spi_message *message)
  1201. {
  1202. struct spi_master *master = spi->master;
  1203. int ret;
  1204. unsigned long flags;
  1205. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1206. if (master->bus_lock_flag)
  1207. ret = -EBUSY;
  1208. else
  1209. ret = __spi_async(spi, message);
  1210. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1211. return ret;
  1212. }
  1213. EXPORT_SYMBOL_GPL(spi_async);
  1214. /**
  1215. * spi_async_locked - version of spi_async with exclusive bus usage
  1216. * @spi: device with which data will be exchanged
  1217. * @message: describes the data transfers, including completion callback
  1218. * Context: any (irqs may be blocked, etc)
  1219. *
  1220. * This call may be used in_irq and other contexts which can't sleep,
  1221. * as well as from task contexts which can sleep.
  1222. *
  1223. * The completion callback is invoked in a context which can't sleep.
  1224. * Before that invocation, the value of message->status is undefined.
  1225. * When the callback is issued, message->status holds either zero (to
  1226. * indicate complete success) or a negative error code. After that
  1227. * callback returns, the driver which issued the transfer request may
  1228. * deallocate the associated memory; it's no longer in use by any SPI
  1229. * core or controller driver code.
  1230. *
  1231. * Note that although all messages to a spi_device are handled in
  1232. * FIFO order, messages may go to different devices in other orders.
  1233. * Some device might be higher priority, or have various "hard" access
  1234. * time requirements, for example.
  1235. *
  1236. * On detection of any fault during the transfer, processing of
  1237. * the entire message is aborted, and the device is deselected.
  1238. * Until returning from the associated message completion callback,
  1239. * no other spi_message queued to that device will be processed.
  1240. * (This rule applies equally to all the synchronous transfer calls,
  1241. * which are wrappers around this core asynchronous primitive.)
  1242. */
  1243. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  1244. {
  1245. struct spi_master *master = spi->master;
  1246. int ret;
  1247. unsigned long flags;
  1248. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1249. ret = __spi_async(spi, message);
  1250. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1251. return ret;
  1252. }
  1253. EXPORT_SYMBOL_GPL(spi_async_locked);
  1254. /*-------------------------------------------------------------------------*/
  1255. /* Utility methods for SPI master protocol drivers, layered on
  1256. * top of the core. Some other utility methods are defined as
  1257. * inline functions.
  1258. */
  1259. static void spi_complete(void *arg)
  1260. {
  1261. complete(arg);
  1262. }
  1263. static int __spi_sync(struct spi_device *spi, struct spi_message *message,
  1264. int bus_locked)
  1265. {
  1266. DECLARE_COMPLETION_ONSTACK(done);
  1267. int status;
  1268. struct spi_master *master = spi->master;
  1269. message->complete = spi_complete;
  1270. message->context = &done;
  1271. if (!bus_locked)
  1272. mutex_lock(&master->bus_lock_mutex);
  1273. status = spi_async_locked(spi, message);
  1274. if (!bus_locked)
  1275. mutex_unlock(&master->bus_lock_mutex);
  1276. if (status == 0) {
  1277. wait_for_completion(&done);
  1278. status = message->status;
  1279. }
  1280. message->context = NULL;
  1281. return status;
  1282. }
  1283. /**
  1284. * spi_sync - blocking/synchronous SPI data transfers
  1285. * @spi: device with which data will be exchanged
  1286. * @message: describes the data transfers
  1287. * Context: can sleep
  1288. *
  1289. * This call may only be used from a context that may sleep. The sleep
  1290. * is non-interruptible, and has no timeout. Low-overhead controller
  1291. * drivers may DMA directly into and out of the message buffers.
  1292. *
  1293. * Note that the SPI device's chip select is active during the message,
  1294. * and then is normally disabled between messages. Drivers for some
  1295. * frequently-used devices may want to minimize costs of selecting a chip,
  1296. * by leaving it selected in anticipation that the next message will go
  1297. * to the same chip. (That may increase power usage.)
  1298. *
  1299. * Also, the caller is guaranteeing that the memory associated with the
  1300. * message will not be freed before this call returns.
  1301. *
  1302. * It returns zero on success, else a negative error code.
  1303. */
  1304. int spi_sync(struct spi_device *spi, struct spi_message *message)
  1305. {
  1306. return __spi_sync(spi, message, 0);
  1307. }
  1308. EXPORT_SYMBOL_GPL(spi_sync);
  1309. /**
  1310. * spi_sync_locked - version of spi_sync with exclusive bus usage
  1311. * @spi: device with which data will be exchanged
  1312. * @message: describes the data transfers
  1313. * Context: can sleep
  1314. *
  1315. * This call may only be used from a context that may sleep. The sleep
  1316. * is non-interruptible, and has no timeout. Low-overhead controller
  1317. * drivers may DMA directly into and out of the message buffers.
  1318. *
  1319. * This call should be used by drivers that require exclusive access to the
  1320. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  1321. * be released by a spi_bus_unlock call when the exclusive access is over.
  1322. *
  1323. * It returns zero on success, else a negative error code.
  1324. */
  1325. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  1326. {
  1327. return __spi_sync(spi, message, 1);
  1328. }
  1329. EXPORT_SYMBOL_GPL(spi_sync_locked);
  1330. /**
  1331. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  1332. * @master: SPI bus master that should be locked for exclusive bus access
  1333. * Context: can sleep
  1334. *
  1335. * This call may only be used from a context that may sleep. The sleep
  1336. * is non-interruptible, and has no timeout.
  1337. *
  1338. * This call should be used by drivers that require exclusive access to the
  1339. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  1340. * exclusive access is over. Data transfer must be done by spi_sync_locked
  1341. * and spi_async_locked calls when the SPI bus lock is held.
  1342. *
  1343. * It returns zero on success, else a negative error code.
  1344. */
  1345. int spi_bus_lock(struct spi_master *master)
  1346. {
  1347. unsigned long flags;
  1348. mutex_lock(&master->bus_lock_mutex);
  1349. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  1350. master->bus_lock_flag = 1;
  1351. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  1352. /* mutex remains locked until spi_bus_unlock is called */
  1353. return 0;
  1354. }
  1355. EXPORT_SYMBOL_GPL(spi_bus_lock);
  1356. /**
  1357. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  1358. * @master: SPI bus master that was locked for exclusive bus access
  1359. * Context: can sleep
  1360. *
  1361. * This call may only be used from a context that may sleep. The sleep
  1362. * is non-interruptible, and has no timeout.
  1363. *
  1364. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  1365. * call.
  1366. *
  1367. * It returns zero on success, else a negative error code.
  1368. */
  1369. int spi_bus_unlock(struct spi_master *master)
  1370. {
  1371. master->bus_lock_flag = 0;
  1372. mutex_unlock(&master->bus_lock_mutex);
  1373. return 0;
  1374. }
  1375. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  1376. /* portable code must never pass more than 32 bytes */
  1377. #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
  1378. static u8 *buf;
  1379. /**
  1380. * spi_write_then_read - SPI synchronous write followed by read
  1381. * @spi: device with which data will be exchanged
  1382. * @txbuf: data to be written (need not be dma-safe)
  1383. * @n_tx: size of txbuf, in bytes
  1384. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  1385. * @n_rx: size of rxbuf, in bytes
  1386. * Context: can sleep
  1387. *
  1388. * This performs a half duplex MicroWire style transaction with the
  1389. * device, sending txbuf and then reading rxbuf. The return value
  1390. * is zero for success, else a negative errno status code.
  1391. * This call may only be used from a context that may sleep.
  1392. *
  1393. * Parameters to this routine are always copied using a small buffer;
  1394. * portable code should never use this for more than 32 bytes.
  1395. * Performance-sensitive or bulk transfer code should instead use
  1396. * spi_{async,sync}() calls with dma-safe buffers.
  1397. */
  1398. int spi_write_then_read(struct spi_device *spi,
  1399. const void *txbuf, unsigned n_tx,
  1400. void *rxbuf, unsigned n_rx)
  1401. {
  1402. static DEFINE_MUTEX(lock);
  1403. int status;
  1404. struct spi_message message;
  1405. struct spi_transfer x[2];
  1406. u8 *local_buf;
  1407. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  1408. * copying here, (as a pure convenience thing), but we can
  1409. * keep heap costs out of the hot path unless someone else is
  1410. * using the pre-allocated buffer or the transfer is too large.
  1411. */
  1412. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  1413. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  1414. GFP_KERNEL | GFP_DMA);
  1415. if (!local_buf)
  1416. return -ENOMEM;
  1417. } else {
  1418. local_buf = buf;
  1419. }
  1420. spi_message_init(&message);
  1421. memset(x, 0, sizeof x);
  1422. if (n_tx) {
  1423. x[0].len = n_tx;
  1424. spi_message_add_tail(&x[0], &message);
  1425. }
  1426. if (n_rx) {
  1427. x[1].len = n_rx;
  1428. spi_message_add_tail(&x[1], &message);
  1429. }
  1430. memcpy(local_buf, txbuf, n_tx);
  1431. x[0].tx_buf = local_buf;
  1432. x[1].rx_buf = local_buf + n_tx;
  1433. /* do the i/o */
  1434. status = spi_sync(spi, &message);
  1435. if (status == 0)
  1436. memcpy(rxbuf, x[1].rx_buf, n_rx);
  1437. if (x[0].tx_buf == buf)
  1438. mutex_unlock(&lock);
  1439. else
  1440. kfree(local_buf);
  1441. return status;
  1442. }
  1443. EXPORT_SYMBOL_GPL(spi_write_then_read);
  1444. /*-------------------------------------------------------------------------*/
  1445. static int __init spi_init(void)
  1446. {
  1447. int status;
  1448. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  1449. if (!buf) {
  1450. status = -ENOMEM;
  1451. goto err0;
  1452. }
  1453. status = bus_register(&spi_bus_type);
  1454. if (status < 0)
  1455. goto err1;
  1456. status = class_register(&spi_master_class);
  1457. if (status < 0)
  1458. goto err2;
  1459. return 0;
  1460. err2:
  1461. bus_unregister(&spi_bus_type);
  1462. err1:
  1463. kfree(buf);
  1464. buf = NULL;
  1465. err0:
  1466. return status;
  1467. }
  1468. /* board_info is normally registered in arch_initcall(),
  1469. * but even essential drivers wait till later
  1470. *
  1471. * REVISIT only boardinfo really needs static linking. the rest (device and
  1472. * driver registration) _could_ be dynamically linked (modular) ... costs
  1473. * include needing to have boardinfo data structures be much more public.
  1474. */
  1475. postcore_initcall(spi_init);