spi.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176
  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. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/cache.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/mutex.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/clk/clk-conf.h>
  27. #include <linux/slab.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/pm_domain.h>
  33. #include <linux/export.h>
  34. #include <linux/sched/rt.h>
  35. #include <linux/delay.h>
  36. #include <linux/kthread.h>
  37. #include <linux/ioport.h>
  38. #include <linux/acpi.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/spi.h>
  41. static void spidev_release(struct device *dev)
  42. {
  43. struct spi_device *spi = to_spi_device(dev);
  44. /* spi masters may cleanup for released devices */
  45. if (spi->master->cleanup)
  46. spi->master->cleanup(spi);
  47. spi_master_put(spi->master);
  48. kfree(spi);
  49. }
  50. static ssize_t
  51. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  52. {
  53. const struct spi_device *spi = to_spi_device(dev);
  54. int len;
  55. len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
  56. if (len != -ENODEV)
  57. return len;
  58. return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
  59. }
  60. static DEVICE_ATTR_RO(modalias);
  61. #define SPI_STATISTICS_ATTRS(field, file) \
  62. static ssize_t spi_master_##field##_show(struct device *dev, \
  63. struct device_attribute *attr, \
  64. char *buf) \
  65. { \
  66. struct spi_master *master = container_of(dev, \
  67. struct spi_master, dev); \
  68. return spi_statistics_##field##_show(&master->statistics, buf); \
  69. } \
  70. static struct device_attribute dev_attr_spi_master_##field = { \
  71. .attr = { .name = file, .mode = S_IRUGO }, \
  72. .show = spi_master_##field##_show, \
  73. }; \
  74. static ssize_t spi_device_##field##_show(struct device *dev, \
  75. struct device_attribute *attr, \
  76. char *buf) \
  77. { \
  78. struct spi_device *spi = to_spi_device(dev); \
  79. return spi_statistics_##field##_show(&spi->statistics, buf); \
  80. } \
  81. static struct device_attribute dev_attr_spi_device_##field = { \
  82. .attr = { .name = file, .mode = S_IRUGO }, \
  83. .show = spi_device_##field##_show, \
  84. }
  85. #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
  86. static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
  87. char *buf) \
  88. { \
  89. unsigned long flags; \
  90. ssize_t len; \
  91. spin_lock_irqsave(&stat->lock, flags); \
  92. len = sprintf(buf, format_string, stat->field); \
  93. spin_unlock_irqrestore(&stat->lock, flags); \
  94. return len; \
  95. } \
  96. SPI_STATISTICS_ATTRS(name, file)
  97. #define SPI_STATISTICS_SHOW(field, format_string) \
  98. SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
  99. field, format_string)
  100. SPI_STATISTICS_SHOW(messages, "%lu");
  101. SPI_STATISTICS_SHOW(transfers, "%lu");
  102. SPI_STATISTICS_SHOW(errors, "%lu");
  103. SPI_STATISTICS_SHOW(timedout, "%lu");
  104. SPI_STATISTICS_SHOW(spi_sync, "%lu");
  105. SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
  106. SPI_STATISTICS_SHOW(spi_async, "%lu");
  107. SPI_STATISTICS_SHOW(bytes, "%llu");
  108. SPI_STATISTICS_SHOW(bytes_rx, "%llu");
  109. SPI_STATISTICS_SHOW(bytes_tx, "%llu");
  110. #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
  111. SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
  112. "transfer_bytes_histo_" number, \
  113. transfer_bytes_histo[index], "%lu")
  114. SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
  115. SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
  116. SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
  117. SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
  118. SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
  119. SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
  120. SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
  121. SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
  122. SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
  123. SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
  124. SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
  125. SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
  126. SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
  127. SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
  128. SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
  129. SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
  130. SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
  131. SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
  132. static struct attribute *spi_dev_attrs[] = {
  133. &dev_attr_modalias.attr,
  134. NULL,
  135. };
  136. static const struct attribute_group spi_dev_group = {
  137. .attrs = spi_dev_attrs,
  138. };
  139. static struct attribute *spi_device_statistics_attrs[] = {
  140. &dev_attr_spi_device_messages.attr,
  141. &dev_attr_spi_device_transfers.attr,
  142. &dev_attr_spi_device_errors.attr,
  143. &dev_attr_spi_device_timedout.attr,
  144. &dev_attr_spi_device_spi_sync.attr,
  145. &dev_attr_spi_device_spi_sync_immediate.attr,
  146. &dev_attr_spi_device_spi_async.attr,
  147. &dev_attr_spi_device_bytes.attr,
  148. &dev_attr_spi_device_bytes_rx.attr,
  149. &dev_attr_spi_device_bytes_tx.attr,
  150. &dev_attr_spi_device_transfer_bytes_histo0.attr,
  151. &dev_attr_spi_device_transfer_bytes_histo1.attr,
  152. &dev_attr_spi_device_transfer_bytes_histo2.attr,
  153. &dev_attr_spi_device_transfer_bytes_histo3.attr,
  154. &dev_attr_spi_device_transfer_bytes_histo4.attr,
  155. &dev_attr_spi_device_transfer_bytes_histo5.attr,
  156. &dev_attr_spi_device_transfer_bytes_histo6.attr,
  157. &dev_attr_spi_device_transfer_bytes_histo7.attr,
  158. &dev_attr_spi_device_transfer_bytes_histo8.attr,
  159. &dev_attr_spi_device_transfer_bytes_histo9.attr,
  160. &dev_attr_spi_device_transfer_bytes_histo10.attr,
  161. &dev_attr_spi_device_transfer_bytes_histo11.attr,
  162. &dev_attr_spi_device_transfer_bytes_histo12.attr,
  163. &dev_attr_spi_device_transfer_bytes_histo13.attr,
  164. &dev_attr_spi_device_transfer_bytes_histo14.attr,
  165. &dev_attr_spi_device_transfer_bytes_histo15.attr,
  166. &dev_attr_spi_device_transfer_bytes_histo16.attr,
  167. &dev_attr_spi_device_transfers_split_maxsize.attr,
  168. NULL,
  169. };
  170. static const struct attribute_group spi_device_statistics_group = {
  171. .name = "statistics",
  172. .attrs = spi_device_statistics_attrs,
  173. };
  174. static const struct attribute_group *spi_dev_groups[] = {
  175. &spi_dev_group,
  176. &spi_device_statistics_group,
  177. NULL,
  178. };
  179. static struct attribute *spi_master_statistics_attrs[] = {
  180. &dev_attr_spi_master_messages.attr,
  181. &dev_attr_spi_master_transfers.attr,
  182. &dev_attr_spi_master_errors.attr,
  183. &dev_attr_spi_master_timedout.attr,
  184. &dev_attr_spi_master_spi_sync.attr,
  185. &dev_attr_spi_master_spi_sync_immediate.attr,
  186. &dev_attr_spi_master_spi_async.attr,
  187. &dev_attr_spi_master_bytes.attr,
  188. &dev_attr_spi_master_bytes_rx.attr,
  189. &dev_attr_spi_master_bytes_tx.attr,
  190. &dev_attr_spi_master_transfer_bytes_histo0.attr,
  191. &dev_attr_spi_master_transfer_bytes_histo1.attr,
  192. &dev_attr_spi_master_transfer_bytes_histo2.attr,
  193. &dev_attr_spi_master_transfer_bytes_histo3.attr,
  194. &dev_attr_spi_master_transfer_bytes_histo4.attr,
  195. &dev_attr_spi_master_transfer_bytes_histo5.attr,
  196. &dev_attr_spi_master_transfer_bytes_histo6.attr,
  197. &dev_attr_spi_master_transfer_bytes_histo7.attr,
  198. &dev_attr_spi_master_transfer_bytes_histo8.attr,
  199. &dev_attr_spi_master_transfer_bytes_histo9.attr,
  200. &dev_attr_spi_master_transfer_bytes_histo10.attr,
  201. &dev_attr_spi_master_transfer_bytes_histo11.attr,
  202. &dev_attr_spi_master_transfer_bytes_histo12.attr,
  203. &dev_attr_spi_master_transfer_bytes_histo13.attr,
  204. &dev_attr_spi_master_transfer_bytes_histo14.attr,
  205. &dev_attr_spi_master_transfer_bytes_histo15.attr,
  206. &dev_attr_spi_master_transfer_bytes_histo16.attr,
  207. &dev_attr_spi_master_transfers_split_maxsize.attr,
  208. NULL,
  209. };
  210. static const struct attribute_group spi_master_statistics_group = {
  211. .name = "statistics",
  212. .attrs = spi_master_statistics_attrs,
  213. };
  214. static const struct attribute_group *spi_master_groups[] = {
  215. &spi_master_statistics_group,
  216. NULL,
  217. };
  218. void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
  219. struct spi_transfer *xfer,
  220. struct spi_master *master)
  221. {
  222. unsigned long flags;
  223. int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
  224. if (l2len < 0)
  225. l2len = 0;
  226. spin_lock_irqsave(&stats->lock, flags);
  227. stats->transfers++;
  228. stats->transfer_bytes_histo[l2len]++;
  229. stats->bytes += xfer->len;
  230. if ((xfer->tx_buf) &&
  231. (xfer->tx_buf != master->dummy_tx))
  232. stats->bytes_tx += xfer->len;
  233. if ((xfer->rx_buf) &&
  234. (xfer->rx_buf != master->dummy_rx))
  235. stats->bytes_rx += xfer->len;
  236. spin_unlock_irqrestore(&stats->lock, flags);
  237. }
  238. EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
  239. /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
  240. * and the sysfs version makes coldplug work too.
  241. */
  242. static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
  243. const struct spi_device *sdev)
  244. {
  245. while (id->name[0]) {
  246. if (!strcmp(sdev->modalias, id->name))
  247. return id;
  248. id++;
  249. }
  250. return NULL;
  251. }
  252. const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
  253. {
  254. const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
  255. return spi_match_id(sdrv->id_table, sdev);
  256. }
  257. EXPORT_SYMBOL_GPL(spi_get_device_id);
  258. static int spi_match_device(struct device *dev, struct device_driver *drv)
  259. {
  260. const struct spi_device *spi = to_spi_device(dev);
  261. const struct spi_driver *sdrv = to_spi_driver(drv);
  262. /* Attempt an OF style match */
  263. if (of_driver_match_device(dev, drv))
  264. return 1;
  265. /* Then try ACPI */
  266. if (acpi_driver_match_device(dev, drv))
  267. return 1;
  268. if (sdrv->id_table)
  269. return !!spi_match_id(sdrv->id_table, spi);
  270. return strcmp(spi->modalias, drv->name) == 0;
  271. }
  272. static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
  273. {
  274. const struct spi_device *spi = to_spi_device(dev);
  275. int rc;
  276. rc = acpi_device_uevent_modalias(dev, env);
  277. if (rc != -ENODEV)
  278. return rc;
  279. add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
  280. return 0;
  281. }
  282. struct bus_type spi_bus_type = {
  283. .name = "spi",
  284. .dev_groups = spi_dev_groups,
  285. .match = spi_match_device,
  286. .uevent = spi_uevent,
  287. };
  288. EXPORT_SYMBOL_GPL(spi_bus_type);
  289. static int spi_drv_probe(struct device *dev)
  290. {
  291. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  292. struct spi_device *spi = to_spi_device(dev);
  293. int ret;
  294. ret = of_clk_set_defaults(dev->of_node, false);
  295. if (ret)
  296. return ret;
  297. if (dev->of_node) {
  298. spi->irq = of_irq_get(dev->of_node, 0);
  299. if (spi->irq == -EPROBE_DEFER)
  300. return -EPROBE_DEFER;
  301. if (spi->irq < 0)
  302. spi->irq = 0;
  303. }
  304. ret = dev_pm_domain_attach(dev, true);
  305. if (ret != -EPROBE_DEFER) {
  306. ret = sdrv->probe(spi);
  307. if (ret)
  308. dev_pm_domain_detach(dev, true);
  309. }
  310. return ret;
  311. }
  312. static int spi_drv_remove(struct device *dev)
  313. {
  314. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  315. int ret;
  316. ret = sdrv->remove(to_spi_device(dev));
  317. dev_pm_domain_detach(dev, true);
  318. return ret;
  319. }
  320. static void spi_drv_shutdown(struct device *dev)
  321. {
  322. const struct spi_driver *sdrv = to_spi_driver(dev->driver);
  323. sdrv->shutdown(to_spi_device(dev));
  324. }
  325. /**
  326. * __spi_register_driver - register a SPI driver
  327. * @owner: owner module of the driver to register
  328. * @sdrv: the driver to register
  329. * Context: can sleep
  330. *
  331. * Return: zero on success, else a negative error code.
  332. */
  333. int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
  334. {
  335. sdrv->driver.owner = owner;
  336. sdrv->driver.bus = &spi_bus_type;
  337. if (sdrv->probe)
  338. sdrv->driver.probe = spi_drv_probe;
  339. if (sdrv->remove)
  340. sdrv->driver.remove = spi_drv_remove;
  341. if (sdrv->shutdown)
  342. sdrv->driver.shutdown = spi_drv_shutdown;
  343. return driver_register(&sdrv->driver);
  344. }
  345. EXPORT_SYMBOL_GPL(__spi_register_driver);
  346. /*-------------------------------------------------------------------------*/
  347. /* SPI devices should normally not be created by SPI device drivers; that
  348. * would make them board-specific. Similarly with SPI master drivers.
  349. * Device registration normally goes into like arch/.../mach.../board-YYY.c
  350. * with other readonly (flashable) information about mainboard devices.
  351. */
  352. struct boardinfo {
  353. struct list_head list;
  354. struct spi_board_info board_info;
  355. };
  356. static LIST_HEAD(board_list);
  357. static LIST_HEAD(spi_master_list);
  358. /*
  359. * Used to protect add/del opertion for board_info list and
  360. * spi_master list, and their matching process
  361. */
  362. static DEFINE_MUTEX(board_lock);
  363. /**
  364. * spi_alloc_device - Allocate a new SPI device
  365. * @master: Controller to which device is connected
  366. * Context: can sleep
  367. *
  368. * Allows a driver to allocate and initialize a spi_device without
  369. * registering it immediately. This allows a driver to directly
  370. * fill the spi_device with device parameters before calling
  371. * spi_add_device() on it.
  372. *
  373. * Caller is responsible to call spi_add_device() on the returned
  374. * spi_device structure to add it to the SPI master. If the caller
  375. * needs to discard the spi_device without adding it, then it should
  376. * call spi_dev_put() on it.
  377. *
  378. * Return: a pointer to the new device, or NULL.
  379. */
  380. struct spi_device *spi_alloc_device(struct spi_master *master)
  381. {
  382. struct spi_device *spi;
  383. if (!spi_master_get(master))
  384. return NULL;
  385. spi = kzalloc(sizeof(*spi), GFP_KERNEL);
  386. if (!spi) {
  387. spi_master_put(master);
  388. return NULL;
  389. }
  390. spi->master = master;
  391. spi->dev.parent = &master->dev;
  392. spi->dev.bus = &spi_bus_type;
  393. spi->dev.release = spidev_release;
  394. spi->cs_gpio = -ENOENT;
  395. spin_lock_init(&spi->statistics.lock);
  396. device_initialize(&spi->dev);
  397. return spi;
  398. }
  399. EXPORT_SYMBOL_GPL(spi_alloc_device);
  400. static void spi_dev_set_name(struct spi_device *spi)
  401. {
  402. struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
  403. if (adev) {
  404. dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
  405. return;
  406. }
  407. dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
  408. spi->chip_select);
  409. }
  410. static int spi_dev_check(struct device *dev, void *data)
  411. {
  412. struct spi_device *spi = to_spi_device(dev);
  413. struct spi_device *new_spi = data;
  414. if (spi->master == new_spi->master &&
  415. spi->chip_select == new_spi->chip_select)
  416. return -EBUSY;
  417. return 0;
  418. }
  419. /**
  420. * spi_add_device - Add spi_device allocated with spi_alloc_device
  421. * @spi: spi_device to register
  422. *
  423. * Companion function to spi_alloc_device. Devices allocated with
  424. * spi_alloc_device can be added onto the spi bus with this function.
  425. *
  426. * Return: 0 on success; negative errno on failure
  427. */
  428. int spi_add_device(struct spi_device *spi)
  429. {
  430. static DEFINE_MUTEX(spi_add_lock);
  431. struct spi_master *master = spi->master;
  432. struct device *dev = master->dev.parent;
  433. int status;
  434. /* Chipselects are numbered 0..max; validate. */
  435. if (spi->chip_select >= master->num_chipselect) {
  436. dev_err(dev, "cs%d >= max %d\n",
  437. spi->chip_select,
  438. master->num_chipselect);
  439. return -EINVAL;
  440. }
  441. /* Set the bus ID string */
  442. spi_dev_set_name(spi);
  443. /* We need to make sure there's no other device with this
  444. * chipselect **BEFORE** we call setup(), else we'll trash
  445. * its configuration. Lock against concurrent add() calls.
  446. */
  447. mutex_lock(&spi_add_lock);
  448. status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
  449. if (status) {
  450. dev_err(dev, "chipselect %d already in use\n",
  451. spi->chip_select);
  452. goto done;
  453. }
  454. if (master->cs_gpios)
  455. spi->cs_gpio = master->cs_gpios[spi->chip_select];
  456. /* Drivers may modify this initial i/o setup, but will
  457. * normally rely on the device being setup. Devices
  458. * using SPI_CS_HIGH can't coexist well otherwise...
  459. */
  460. status = spi_setup(spi);
  461. if (status < 0) {
  462. dev_err(dev, "can't setup %s, status %d\n",
  463. dev_name(&spi->dev), status);
  464. goto done;
  465. }
  466. /* Device may be bound to an active driver when this returns */
  467. status = device_add(&spi->dev);
  468. if (status < 0)
  469. dev_err(dev, "can't add %s, status %d\n",
  470. dev_name(&spi->dev), status);
  471. else
  472. dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
  473. done:
  474. mutex_unlock(&spi_add_lock);
  475. return status;
  476. }
  477. EXPORT_SYMBOL_GPL(spi_add_device);
  478. /**
  479. * spi_new_device - instantiate one new SPI device
  480. * @master: Controller to which device is connected
  481. * @chip: Describes the SPI device
  482. * Context: can sleep
  483. *
  484. * On typical mainboards, this is purely internal; and it's not needed
  485. * after board init creates the hard-wired devices. Some development
  486. * platforms may not be able to use spi_register_board_info though, and
  487. * this is exported so that for example a USB or parport based adapter
  488. * driver could add devices (which it would learn about out-of-band).
  489. *
  490. * Return: the new device, or NULL.
  491. */
  492. struct spi_device *spi_new_device(struct spi_master *master,
  493. struct spi_board_info *chip)
  494. {
  495. struct spi_device *proxy;
  496. int status;
  497. /* NOTE: caller did any chip->bus_num checks necessary.
  498. *
  499. * Also, unless we change the return value convention to use
  500. * error-or-pointer (not NULL-or-pointer), troubleshootability
  501. * suggests syslogged diagnostics are best here (ugh).
  502. */
  503. proxy = spi_alloc_device(master);
  504. if (!proxy)
  505. return NULL;
  506. WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
  507. proxy->chip_select = chip->chip_select;
  508. proxy->max_speed_hz = chip->max_speed_hz;
  509. proxy->mode = chip->mode;
  510. proxy->irq = chip->irq;
  511. strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
  512. proxy->dev.platform_data = (void *) chip->platform_data;
  513. proxy->controller_data = chip->controller_data;
  514. proxy->controller_state = NULL;
  515. status = spi_add_device(proxy);
  516. if (status < 0) {
  517. spi_dev_put(proxy);
  518. return NULL;
  519. }
  520. return proxy;
  521. }
  522. EXPORT_SYMBOL_GPL(spi_new_device);
  523. /**
  524. * spi_unregister_device - unregister a single SPI device
  525. * @spi: spi_device to unregister
  526. *
  527. * Start making the passed SPI device vanish. Normally this would be handled
  528. * by spi_unregister_master().
  529. */
  530. void spi_unregister_device(struct spi_device *spi)
  531. {
  532. if (!spi)
  533. return;
  534. if (spi->dev.of_node)
  535. of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
  536. device_unregister(&spi->dev);
  537. }
  538. EXPORT_SYMBOL_GPL(spi_unregister_device);
  539. static void spi_match_master_to_boardinfo(struct spi_master *master,
  540. struct spi_board_info *bi)
  541. {
  542. struct spi_device *dev;
  543. if (master->bus_num != bi->bus_num)
  544. return;
  545. dev = spi_new_device(master, bi);
  546. if (!dev)
  547. dev_err(master->dev.parent, "can't create new device for %s\n",
  548. bi->modalias);
  549. }
  550. /**
  551. * spi_register_board_info - register SPI devices for a given board
  552. * @info: array of chip descriptors
  553. * @n: how many descriptors are provided
  554. * Context: can sleep
  555. *
  556. * Board-specific early init code calls this (probably during arch_initcall)
  557. * with segments of the SPI device table. Any device nodes are created later,
  558. * after the relevant parent SPI controller (bus_num) is defined. We keep
  559. * this table of devices forever, so that reloading a controller driver will
  560. * not make Linux forget about these hard-wired devices.
  561. *
  562. * Other code can also call this, e.g. a particular add-on board might provide
  563. * SPI devices through its expansion connector, so code initializing that board
  564. * would naturally declare its SPI devices.
  565. *
  566. * The board info passed can safely be __initdata ... but be careful of
  567. * any embedded pointers (platform_data, etc), they're copied as-is.
  568. *
  569. * Return: zero on success, else a negative error code.
  570. */
  571. int spi_register_board_info(struct spi_board_info const *info, unsigned n)
  572. {
  573. struct boardinfo *bi;
  574. int i;
  575. if (!n)
  576. return -EINVAL;
  577. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  578. if (!bi)
  579. return -ENOMEM;
  580. for (i = 0; i < n; i++, bi++, info++) {
  581. struct spi_master *master;
  582. memcpy(&bi->board_info, info, sizeof(*info));
  583. mutex_lock(&board_lock);
  584. list_add_tail(&bi->list, &board_list);
  585. list_for_each_entry(master, &spi_master_list, list)
  586. spi_match_master_to_boardinfo(master, &bi->board_info);
  587. mutex_unlock(&board_lock);
  588. }
  589. return 0;
  590. }
  591. /*-------------------------------------------------------------------------*/
  592. static void spi_set_cs(struct spi_device *spi, bool enable)
  593. {
  594. if (spi->mode & SPI_CS_HIGH)
  595. enable = !enable;
  596. if (gpio_is_valid(spi->cs_gpio))
  597. gpio_set_value(spi->cs_gpio, !enable);
  598. else if (spi->master->set_cs)
  599. spi->master->set_cs(spi, !enable);
  600. }
  601. #ifdef CONFIG_HAS_DMA
  602. static int spi_map_buf(struct spi_master *master, struct device *dev,
  603. struct sg_table *sgt, void *buf, size_t len,
  604. enum dma_data_direction dir)
  605. {
  606. const bool vmalloced_buf = is_vmalloc_addr(buf);
  607. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  608. int desc_len;
  609. int sgs;
  610. struct page *vm_page;
  611. void *sg_buf;
  612. size_t min;
  613. int i, ret;
  614. if (vmalloced_buf) {
  615. desc_len = min_t(int, max_seg_size, PAGE_SIZE);
  616. sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
  617. } else if (virt_addr_valid(buf)) {
  618. desc_len = min_t(int, max_seg_size, master->max_dma_len);
  619. sgs = DIV_ROUND_UP(len, desc_len);
  620. } else {
  621. return -EINVAL;
  622. }
  623. ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
  624. if (ret != 0)
  625. return ret;
  626. for (i = 0; i < sgs; i++) {
  627. if (vmalloced_buf) {
  628. min = min_t(size_t,
  629. len, desc_len - offset_in_page(buf));
  630. vm_page = vmalloc_to_page(buf);
  631. if (!vm_page) {
  632. sg_free_table(sgt);
  633. return -ENOMEM;
  634. }
  635. sg_set_page(&sgt->sgl[i], vm_page,
  636. min, offset_in_page(buf));
  637. } else {
  638. min = min_t(size_t, len, desc_len);
  639. sg_buf = buf;
  640. sg_set_buf(&sgt->sgl[i], sg_buf, min);
  641. }
  642. buf += min;
  643. len -= min;
  644. }
  645. ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
  646. if (!ret)
  647. ret = -ENOMEM;
  648. if (ret < 0) {
  649. sg_free_table(sgt);
  650. return ret;
  651. }
  652. sgt->nents = ret;
  653. return 0;
  654. }
  655. static void spi_unmap_buf(struct spi_master *master, struct device *dev,
  656. struct sg_table *sgt, enum dma_data_direction dir)
  657. {
  658. if (sgt->orig_nents) {
  659. dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
  660. sg_free_table(sgt);
  661. }
  662. }
  663. static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
  664. {
  665. struct device *tx_dev, *rx_dev;
  666. struct spi_transfer *xfer;
  667. int ret;
  668. if (!master->can_dma)
  669. return 0;
  670. if (master->dma_tx)
  671. tx_dev = master->dma_tx->device->dev;
  672. else
  673. tx_dev = &master->dev;
  674. if (master->dma_rx)
  675. rx_dev = master->dma_rx->device->dev;
  676. else
  677. rx_dev = &master->dev;
  678. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  679. if (!master->can_dma(master, msg->spi, xfer))
  680. continue;
  681. if (xfer->tx_buf != NULL) {
  682. ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
  683. (void *)xfer->tx_buf, xfer->len,
  684. DMA_TO_DEVICE);
  685. if (ret != 0)
  686. return ret;
  687. }
  688. if (xfer->rx_buf != NULL) {
  689. ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
  690. xfer->rx_buf, xfer->len,
  691. DMA_FROM_DEVICE);
  692. if (ret != 0) {
  693. spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
  694. DMA_TO_DEVICE);
  695. return ret;
  696. }
  697. }
  698. }
  699. master->cur_msg_mapped = true;
  700. return 0;
  701. }
  702. static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
  703. {
  704. struct spi_transfer *xfer;
  705. struct device *tx_dev, *rx_dev;
  706. if (!master->cur_msg_mapped || !master->can_dma)
  707. return 0;
  708. if (master->dma_tx)
  709. tx_dev = master->dma_tx->device->dev;
  710. else
  711. tx_dev = &master->dev;
  712. if (master->dma_rx)
  713. rx_dev = master->dma_rx->device->dev;
  714. else
  715. rx_dev = &master->dev;
  716. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  717. if (!master->can_dma(master, msg->spi, xfer))
  718. continue;
  719. spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
  720. spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
  721. }
  722. return 0;
  723. }
  724. #else /* !CONFIG_HAS_DMA */
  725. static inline int spi_map_buf(struct spi_master *master,
  726. struct device *dev, struct sg_table *sgt,
  727. void *buf, size_t len,
  728. enum dma_data_direction dir)
  729. {
  730. return -EINVAL;
  731. }
  732. static inline void spi_unmap_buf(struct spi_master *master,
  733. struct device *dev, struct sg_table *sgt,
  734. enum dma_data_direction dir)
  735. {
  736. }
  737. static inline int __spi_map_msg(struct spi_master *master,
  738. struct spi_message *msg)
  739. {
  740. return 0;
  741. }
  742. static inline int __spi_unmap_msg(struct spi_master *master,
  743. struct spi_message *msg)
  744. {
  745. return 0;
  746. }
  747. #endif /* !CONFIG_HAS_DMA */
  748. static inline int spi_unmap_msg(struct spi_master *master,
  749. struct spi_message *msg)
  750. {
  751. struct spi_transfer *xfer;
  752. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  753. /*
  754. * Restore the original value of tx_buf or rx_buf if they are
  755. * NULL.
  756. */
  757. if (xfer->tx_buf == master->dummy_tx)
  758. xfer->tx_buf = NULL;
  759. if (xfer->rx_buf == master->dummy_rx)
  760. xfer->rx_buf = NULL;
  761. }
  762. return __spi_unmap_msg(master, msg);
  763. }
  764. static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
  765. {
  766. struct spi_transfer *xfer;
  767. void *tmp;
  768. unsigned int max_tx, max_rx;
  769. if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
  770. max_tx = 0;
  771. max_rx = 0;
  772. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  773. if ((master->flags & SPI_MASTER_MUST_TX) &&
  774. !xfer->tx_buf)
  775. max_tx = max(xfer->len, max_tx);
  776. if ((master->flags & SPI_MASTER_MUST_RX) &&
  777. !xfer->rx_buf)
  778. max_rx = max(xfer->len, max_rx);
  779. }
  780. if (max_tx) {
  781. tmp = krealloc(master->dummy_tx, max_tx,
  782. GFP_KERNEL | GFP_DMA);
  783. if (!tmp)
  784. return -ENOMEM;
  785. master->dummy_tx = tmp;
  786. memset(tmp, 0, max_tx);
  787. }
  788. if (max_rx) {
  789. tmp = krealloc(master->dummy_rx, max_rx,
  790. GFP_KERNEL | GFP_DMA);
  791. if (!tmp)
  792. return -ENOMEM;
  793. master->dummy_rx = tmp;
  794. }
  795. if (max_tx || max_rx) {
  796. list_for_each_entry(xfer, &msg->transfers,
  797. transfer_list) {
  798. if (!xfer->tx_buf)
  799. xfer->tx_buf = master->dummy_tx;
  800. if (!xfer->rx_buf)
  801. xfer->rx_buf = master->dummy_rx;
  802. }
  803. }
  804. }
  805. return __spi_map_msg(master, msg);
  806. }
  807. /*
  808. * spi_transfer_one_message - Default implementation of transfer_one_message()
  809. *
  810. * This is a standard implementation of transfer_one_message() for
  811. * drivers which implement a transfer_one() operation. It provides
  812. * standard handling of delays and chip select management.
  813. */
  814. static int spi_transfer_one_message(struct spi_master *master,
  815. struct spi_message *msg)
  816. {
  817. struct spi_transfer *xfer;
  818. bool keep_cs = false;
  819. int ret = 0;
  820. unsigned long ms = 1;
  821. struct spi_statistics *statm = &master->statistics;
  822. struct spi_statistics *stats = &msg->spi->statistics;
  823. spi_set_cs(msg->spi, true);
  824. SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
  825. SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
  826. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  827. trace_spi_transfer_start(msg, xfer);
  828. spi_statistics_add_transfer_stats(statm, xfer, master);
  829. spi_statistics_add_transfer_stats(stats, xfer, master);
  830. if (xfer->tx_buf || xfer->rx_buf) {
  831. reinit_completion(&master->xfer_completion);
  832. ret = master->transfer_one(master, msg->spi, xfer);
  833. if (ret < 0) {
  834. SPI_STATISTICS_INCREMENT_FIELD(statm,
  835. errors);
  836. SPI_STATISTICS_INCREMENT_FIELD(stats,
  837. errors);
  838. dev_err(&msg->spi->dev,
  839. "SPI transfer failed: %d\n", ret);
  840. goto out;
  841. }
  842. if (ret > 0) {
  843. ret = 0;
  844. ms = xfer->len * 8 * 1000 / xfer->speed_hz;
  845. ms += ms + 100; /* some tolerance */
  846. ms = wait_for_completion_timeout(&master->xfer_completion,
  847. msecs_to_jiffies(ms));
  848. }
  849. if (ms == 0) {
  850. SPI_STATISTICS_INCREMENT_FIELD(statm,
  851. timedout);
  852. SPI_STATISTICS_INCREMENT_FIELD(stats,
  853. timedout);
  854. dev_err(&msg->spi->dev,
  855. "SPI transfer timed out\n");
  856. msg->status = -ETIMEDOUT;
  857. }
  858. } else {
  859. if (xfer->len)
  860. dev_err(&msg->spi->dev,
  861. "Bufferless transfer has length %u\n",
  862. xfer->len);
  863. }
  864. trace_spi_transfer_stop(msg, xfer);
  865. if (msg->status != -EINPROGRESS)
  866. goto out;
  867. if (xfer->delay_usecs)
  868. udelay(xfer->delay_usecs);
  869. if (xfer->cs_change) {
  870. if (list_is_last(&xfer->transfer_list,
  871. &msg->transfers)) {
  872. keep_cs = true;
  873. } else {
  874. spi_set_cs(msg->spi, false);
  875. udelay(10);
  876. spi_set_cs(msg->spi, true);
  877. }
  878. }
  879. msg->actual_length += xfer->len;
  880. }
  881. out:
  882. if (ret != 0 || !keep_cs)
  883. spi_set_cs(msg->spi, false);
  884. if (msg->status == -EINPROGRESS)
  885. msg->status = ret;
  886. if (msg->status && master->handle_err)
  887. master->handle_err(master, msg);
  888. spi_res_release(master, msg);
  889. spi_finalize_current_message(master);
  890. return ret;
  891. }
  892. /**
  893. * spi_finalize_current_transfer - report completion of a transfer
  894. * @master: the master reporting completion
  895. *
  896. * Called by SPI drivers using the core transfer_one_message()
  897. * implementation to notify it that the current interrupt driven
  898. * transfer has finished and the next one may be scheduled.
  899. */
  900. void spi_finalize_current_transfer(struct spi_master *master)
  901. {
  902. complete(&master->xfer_completion);
  903. }
  904. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  905. /**
  906. * __spi_pump_messages - function which processes spi message queue
  907. * @master: master to process queue for
  908. * @in_kthread: true if we are in the context of the message pump thread
  909. *
  910. * This function checks if there is any spi message in the queue that
  911. * needs processing and if so call out to the driver to initialize hardware
  912. * and transfer each message.
  913. *
  914. * Note that it is called both from the kthread itself and also from
  915. * inside spi_sync(); the queue extraction handling at the top of the
  916. * function should deal with this safely.
  917. */
  918. static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
  919. {
  920. unsigned long flags;
  921. bool was_busy = false;
  922. int ret;
  923. /* Lock queue */
  924. spin_lock_irqsave(&master->queue_lock, flags);
  925. /* Make sure we are not already running a message */
  926. if (master->cur_msg) {
  927. spin_unlock_irqrestore(&master->queue_lock, flags);
  928. return;
  929. }
  930. /* If another context is idling the device then defer */
  931. if (master->idling) {
  932. queue_kthread_work(&master->kworker, &master->pump_messages);
  933. spin_unlock_irqrestore(&master->queue_lock, flags);
  934. return;
  935. }
  936. /* Check if the queue is idle */
  937. if (list_empty(&master->queue) || !master->running) {
  938. if (!master->busy) {
  939. spin_unlock_irqrestore(&master->queue_lock, flags);
  940. return;
  941. }
  942. /* Only do teardown in the thread */
  943. if (!in_kthread) {
  944. queue_kthread_work(&master->kworker,
  945. &master->pump_messages);
  946. spin_unlock_irqrestore(&master->queue_lock, flags);
  947. return;
  948. }
  949. master->busy = false;
  950. master->idling = true;
  951. spin_unlock_irqrestore(&master->queue_lock, flags);
  952. kfree(master->dummy_rx);
  953. master->dummy_rx = NULL;
  954. kfree(master->dummy_tx);
  955. master->dummy_tx = NULL;
  956. if (master->unprepare_transfer_hardware &&
  957. master->unprepare_transfer_hardware(master))
  958. dev_err(&master->dev,
  959. "failed to unprepare transfer hardware\n");
  960. if (master->auto_runtime_pm) {
  961. pm_runtime_mark_last_busy(master->dev.parent);
  962. pm_runtime_put_autosuspend(master->dev.parent);
  963. }
  964. trace_spi_master_idle(master);
  965. spin_lock_irqsave(&master->queue_lock, flags);
  966. master->idling = false;
  967. spin_unlock_irqrestore(&master->queue_lock, flags);
  968. return;
  969. }
  970. /* Extract head of queue */
  971. master->cur_msg =
  972. list_first_entry(&master->queue, struct spi_message, queue);
  973. list_del_init(&master->cur_msg->queue);
  974. if (master->busy)
  975. was_busy = true;
  976. else
  977. master->busy = true;
  978. spin_unlock_irqrestore(&master->queue_lock, flags);
  979. mutex_lock(&master->io_mutex);
  980. if (!was_busy && master->auto_runtime_pm) {
  981. ret = pm_runtime_get_sync(master->dev.parent);
  982. if (ret < 0) {
  983. dev_err(&master->dev, "Failed to power device: %d\n",
  984. ret);
  985. return;
  986. }
  987. }
  988. if (!was_busy)
  989. trace_spi_master_busy(master);
  990. if (!was_busy && master->prepare_transfer_hardware) {
  991. ret = master->prepare_transfer_hardware(master);
  992. if (ret) {
  993. dev_err(&master->dev,
  994. "failed to prepare transfer hardware\n");
  995. if (master->auto_runtime_pm)
  996. pm_runtime_put(master->dev.parent);
  997. return;
  998. }
  999. }
  1000. trace_spi_message_start(master->cur_msg);
  1001. if (master->prepare_message) {
  1002. ret = master->prepare_message(master, master->cur_msg);
  1003. if (ret) {
  1004. dev_err(&master->dev,
  1005. "failed to prepare message: %d\n", ret);
  1006. master->cur_msg->status = ret;
  1007. spi_finalize_current_message(master);
  1008. goto out;
  1009. }
  1010. master->cur_msg_prepared = true;
  1011. }
  1012. ret = spi_map_msg(master, master->cur_msg);
  1013. if (ret) {
  1014. master->cur_msg->status = ret;
  1015. spi_finalize_current_message(master);
  1016. goto out;
  1017. }
  1018. ret = master->transfer_one_message(master, master->cur_msg);
  1019. if (ret) {
  1020. dev_err(&master->dev,
  1021. "failed to transfer one message from queue\n");
  1022. goto out;
  1023. }
  1024. out:
  1025. mutex_unlock(&master->io_mutex);
  1026. /* Prod the scheduler in case transfer_one() was busy waiting */
  1027. if (!ret)
  1028. cond_resched();
  1029. }
  1030. /**
  1031. * spi_pump_messages - kthread work function which processes spi message queue
  1032. * @work: pointer to kthread work struct contained in the master struct
  1033. */
  1034. static void spi_pump_messages(struct kthread_work *work)
  1035. {
  1036. struct spi_master *master =
  1037. container_of(work, struct spi_master, pump_messages);
  1038. __spi_pump_messages(master, true);
  1039. }
  1040. static int spi_init_queue(struct spi_master *master)
  1041. {
  1042. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  1043. master->running = false;
  1044. master->busy = false;
  1045. init_kthread_worker(&master->kworker);
  1046. master->kworker_task = kthread_run(kthread_worker_fn,
  1047. &master->kworker, "%s",
  1048. dev_name(&master->dev));
  1049. if (IS_ERR(master->kworker_task)) {
  1050. dev_err(&master->dev, "failed to create message pump task\n");
  1051. return PTR_ERR(master->kworker_task);
  1052. }
  1053. init_kthread_work(&master->pump_messages, spi_pump_messages);
  1054. /*
  1055. * Master config will indicate if this controller should run the
  1056. * message pump with high (realtime) priority to reduce the transfer
  1057. * latency on the bus by minimising the delay between a transfer
  1058. * request and the scheduling of the message pump thread. Without this
  1059. * setting the message pump thread will remain at default priority.
  1060. */
  1061. if (master->rt) {
  1062. dev_info(&master->dev,
  1063. "will run message pump with realtime priority\n");
  1064. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  1065. }
  1066. return 0;
  1067. }
  1068. /**
  1069. * spi_get_next_queued_message() - called by driver to check for queued
  1070. * messages
  1071. * @master: the master to check for queued messages
  1072. *
  1073. * If there are more messages in the queue, the next message is returned from
  1074. * this call.
  1075. *
  1076. * Return: the next message in the queue, else NULL if the queue is empty.
  1077. */
  1078. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  1079. {
  1080. struct spi_message *next;
  1081. unsigned long flags;
  1082. /* get a pointer to the next message, if any */
  1083. spin_lock_irqsave(&master->queue_lock, flags);
  1084. next = list_first_entry_or_null(&master->queue, struct spi_message,
  1085. queue);
  1086. spin_unlock_irqrestore(&master->queue_lock, flags);
  1087. return next;
  1088. }
  1089. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  1090. /**
  1091. * spi_finalize_current_message() - the current message is complete
  1092. * @master: the master to return the message to
  1093. *
  1094. * Called by the driver to notify the core that the message in the front of the
  1095. * queue is complete and can be removed from the queue.
  1096. */
  1097. void spi_finalize_current_message(struct spi_master *master)
  1098. {
  1099. struct spi_message *mesg;
  1100. unsigned long flags;
  1101. int ret;
  1102. spin_lock_irqsave(&master->queue_lock, flags);
  1103. mesg = master->cur_msg;
  1104. spin_unlock_irqrestore(&master->queue_lock, flags);
  1105. spi_unmap_msg(master, mesg);
  1106. if (master->cur_msg_prepared && master->unprepare_message) {
  1107. ret = master->unprepare_message(master, mesg);
  1108. if (ret) {
  1109. dev_err(&master->dev,
  1110. "failed to unprepare message: %d\n", ret);
  1111. }
  1112. }
  1113. spin_lock_irqsave(&master->queue_lock, flags);
  1114. master->cur_msg = NULL;
  1115. master->cur_msg_prepared = false;
  1116. queue_kthread_work(&master->kworker, &master->pump_messages);
  1117. spin_unlock_irqrestore(&master->queue_lock, flags);
  1118. trace_spi_message_done(mesg);
  1119. mesg->state = NULL;
  1120. if (mesg->complete)
  1121. mesg->complete(mesg->context);
  1122. }
  1123. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  1124. static int spi_start_queue(struct spi_master *master)
  1125. {
  1126. unsigned long flags;
  1127. spin_lock_irqsave(&master->queue_lock, flags);
  1128. if (master->running || master->busy) {
  1129. spin_unlock_irqrestore(&master->queue_lock, flags);
  1130. return -EBUSY;
  1131. }
  1132. master->running = true;
  1133. master->cur_msg = NULL;
  1134. spin_unlock_irqrestore(&master->queue_lock, flags);
  1135. queue_kthread_work(&master->kworker, &master->pump_messages);
  1136. return 0;
  1137. }
  1138. static int spi_stop_queue(struct spi_master *master)
  1139. {
  1140. unsigned long flags;
  1141. unsigned limit = 500;
  1142. int ret = 0;
  1143. spin_lock_irqsave(&master->queue_lock, flags);
  1144. /*
  1145. * This is a bit lame, but is optimized for the common execution path.
  1146. * A wait_queue on the master->busy could be used, but then the common
  1147. * execution path (pump_messages) would be required to call wake_up or
  1148. * friends on every SPI message. Do this instead.
  1149. */
  1150. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  1151. spin_unlock_irqrestore(&master->queue_lock, flags);
  1152. usleep_range(10000, 11000);
  1153. spin_lock_irqsave(&master->queue_lock, flags);
  1154. }
  1155. if (!list_empty(&master->queue) || master->busy)
  1156. ret = -EBUSY;
  1157. else
  1158. master->running = false;
  1159. spin_unlock_irqrestore(&master->queue_lock, flags);
  1160. if (ret) {
  1161. dev_warn(&master->dev,
  1162. "could not stop message queue\n");
  1163. return ret;
  1164. }
  1165. return ret;
  1166. }
  1167. static int spi_destroy_queue(struct spi_master *master)
  1168. {
  1169. int ret;
  1170. ret = spi_stop_queue(master);
  1171. /*
  1172. * flush_kthread_worker will block until all work is done.
  1173. * If the reason that stop_queue timed out is that the work will never
  1174. * finish, then it does no good to call flush/stop thread, so
  1175. * return anyway.
  1176. */
  1177. if (ret) {
  1178. dev_err(&master->dev, "problem destroying queue\n");
  1179. return ret;
  1180. }
  1181. flush_kthread_worker(&master->kworker);
  1182. kthread_stop(master->kworker_task);
  1183. return 0;
  1184. }
  1185. static int __spi_queued_transfer(struct spi_device *spi,
  1186. struct spi_message *msg,
  1187. bool need_pump)
  1188. {
  1189. struct spi_master *master = spi->master;
  1190. unsigned long flags;
  1191. spin_lock_irqsave(&master->queue_lock, flags);
  1192. if (!master->running) {
  1193. spin_unlock_irqrestore(&master->queue_lock, flags);
  1194. return -ESHUTDOWN;
  1195. }
  1196. msg->actual_length = 0;
  1197. msg->status = -EINPROGRESS;
  1198. list_add_tail(&msg->queue, &master->queue);
  1199. if (!master->busy && need_pump)
  1200. queue_kthread_work(&master->kworker, &master->pump_messages);
  1201. spin_unlock_irqrestore(&master->queue_lock, flags);
  1202. return 0;
  1203. }
  1204. /**
  1205. * spi_queued_transfer - transfer function for queued transfers
  1206. * @spi: spi device which is requesting transfer
  1207. * @msg: spi message which is to handled is queued to driver queue
  1208. *
  1209. * Return: zero on success, else a negative error code.
  1210. */
  1211. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  1212. {
  1213. return __spi_queued_transfer(spi, msg, true);
  1214. }
  1215. static int spi_master_initialize_queue(struct spi_master *master)
  1216. {
  1217. int ret;
  1218. master->transfer = spi_queued_transfer;
  1219. if (!master->transfer_one_message)
  1220. master->transfer_one_message = spi_transfer_one_message;
  1221. /* Initialize and start queue */
  1222. ret = spi_init_queue(master);
  1223. if (ret) {
  1224. dev_err(&master->dev, "problem initializing queue\n");
  1225. goto err_init_queue;
  1226. }
  1227. master->queued = true;
  1228. ret = spi_start_queue(master);
  1229. if (ret) {
  1230. dev_err(&master->dev, "problem starting queue\n");
  1231. goto err_start_queue;
  1232. }
  1233. return 0;
  1234. err_start_queue:
  1235. spi_destroy_queue(master);
  1236. err_init_queue:
  1237. return ret;
  1238. }
  1239. /*-------------------------------------------------------------------------*/
  1240. #if defined(CONFIG_OF)
  1241. static struct spi_device *
  1242. of_register_spi_device(struct spi_master *master, struct device_node *nc)
  1243. {
  1244. struct spi_device *spi;
  1245. int rc;
  1246. u32 value;
  1247. /* Alloc an spi_device */
  1248. spi = spi_alloc_device(master);
  1249. if (!spi) {
  1250. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1251. nc->full_name);
  1252. rc = -ENOMEM;
  1253. goto err_out;
  1254. }
  1255. /* Select device driver */
  1256. rc = of_modalias_node(nc, spi->modalias,
  1257. sizeof(spi->modalias));
  1258. if (rc < 0) {
  1259. dev_err(&master->dev, "cannot find modalias for %s\n",
  1260. nc->full_name);
  1261. goto err_out;
  1262. }
  1263. /* Device address */
  1264. rc = of_property_read_u32(nc, "reg", &value);
  1265. if (rc) {
  1266. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1267. nc->full_name, rc);
  1268. goto err_out;
  1269. }
  1270. spi->chip_select = value;
  1271. /* Mode (clock phase/polarity/etc.) */
  1272. if (of_find_property(nc, "spi-cpha", NULL))
  1273. spi->mode |= SPI_CPHA;
  1274. if (of_find_property(nc, "spi-cpol", NULL))
  1275. spi->mode |= SPI_CPOL;
  1276. if (of_find_property(nc, "spi-cs-high", NULL))
  1277. spi->mode |= SPI_CS_HIGH;
  1278. if (of_find_property(nc, "spi-3wire", NULL))
  1279. spi->mode |= SPI_3WIRE;
  1280. if (of_find_property(nc, "spi-lsb-first", NULL))
  1281. spi->mode |= SPI_LSB_FIRST;
  1282. /* Device DUAL/QUAD mode */
  1283. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1284. switch (value) {
  1285. case 1:
  1286. break;
  1287. case 2:
  1288. spi->mode |= SPI_TX_DUAL;
  1289. break;
  1290. case 4:
  1291. spi->mode |= SPI_TX_QUAD;
  1292. break;
  1293. default:
  1294. dev_warn(&master->dev,
  1295. "spi-tx-bus-width %d not supported\n",
  1296. value);
  1297. break;
  1298. }
  1299. }
  1300. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1301. switch (value) {
  1302. case 1:
  1303. break;
  1304. case 2:
  1305. spi->mode |= SPI_RX_DUAL;
  1306. break;
  1307. case 4:
  1308. spi->mode |= SPI_RX_QUAD;
  1309. break;
  1310. default:
  1311. dev_warn(&master->dev,
  1312. "spi-rx-bus-width %d not supported\n",
  1313. value);
  1314. break;
  1315. }
  1316. }
  1317. /* Device speed */
  1318. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1319. if (rc) {
  1320. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1321. nc->full_name, rc);
  1322. goto err_out;
  1323. }
  1324. spi->max_speed_hz = value;
  1325. /* Store a pointer to the node in the device structure */
  1326. of_node_get(nc);
  1327. spi->dev.of_node = nc;
  1328. /* Register the new device */
  1329. rc = spi_add_device(spi);
  1330. if (rc) {
  1331. dev_err(&master->dev, "spi_device register error %s\n",
  1332. nc->full_name);
  1333. goto err_out;
  1334. }
  1335. return spi;
  1336. err_out:
  1337. spi_dev_put(spi);
  1338. return ERR_PTR(rc);
  1339. }
  1340. /**
  1341. * of_register_spi_devices() - Register child devices onto the SPI bus
  1342. * @master: Pointer to spi_master device
  1343. *
  1344. * Registers an spi_device for each child node of master node which has a 'reg'
  1345. * property.
  1346. */
  1347. static void of_register_spi_devices(struct spi_master *master)
  1348. {
  1349. struct spi_device *spi;
  1350. struct device_node *nc;
  1351. if (!master->dev.of_node)
  1352. return;
  1353. for_each_available_child_of_node(master->dev.of_node, nc) {
  1354. if (of_node_test_and_set_flag(nc, OF_POPULATED))
  1355. continue;
  1356. spi = of_register_spi_device(master, nc);
  1357. if (IS_ERR(spi))
  1358. dev_warn(&master->dev, "Failed to create SPI device for %s\n",
  1359. nc->full_name);
  1360. }
  1361. }
  1362. #else
  1363. static void of_register_spi_devices(struct spi_master *master) { }
  1364. #endif
  1365. #ifdef CONFIG_ACPI
  1366. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1367. {
  1368. struct spi_device *spi = data;
  1369. struct spi_master *master = spi->master;
  1370. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1371. struct acpi_resource_spi_serialbus *sb;
  1372. sb = &ares->data.spi_serial_bus;
  1373. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1374. /*
  1375. * ACPI DeviceSelection numbering is handled by the
  1376. * host controller driver in Windows and can vary
  1377. * from driver to driver. In Linux we always expect
  1378. * 0 .. max - 1 so we need to ask the driver to
  1379. * translate between the two schemes.
  1380. */
  1381. if (master->fw_translate_cs) {
  1382. int cs = master->fw_translate_cs(master,
  1383. sb->device_selection);
  1384. if (cs < 0)
  1385. return cs;
  1386. spi->chip_select = cs;
  1387. } else {
  1388. spi->chip_select = sb->device_selection;
  1389. }
  1390. spi->max_speed_hz = sb->connection_speed;
  1391. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1392. spi->mode |= SPI_CPHA;
  1393. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1394. spi->mode |= SPI_CPOL;
  1395. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1396. spi->mode |= SPI_CS_HIGH;
  1397. }
  1398. } else if (spi->irq < 0) {
  1399. struct resource r;
  1400. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1401. spi->irq = r.start;
  1402. }
  1403. /* Always tell the ACPI core to skip this resource */
  1404. return 1;
  1405. }
  1406. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1407. void *data, void **return_value)
  1408. {
  1409. struct spi_master *master = data;
  1410. struct list_head resource_list;
  1411. struct acpi_device *adev;
  1412. struct spi_device *spi;
  1413. int ret;
  1414. if (acpi_bus_get_device(handle, &adev))
  1415. return AE_OK;
  1416. if (acpi_bus_get_status(adev) || !adev->status.present)
  1417. return AE_OK;
  1418. spi = spi_alloc_device(master);
  1419. if (!spi) {
  1420. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1421. dev_name(&adev->dev));
  1422. return AE_NO_MEMORY;
  1423. }
  1424. ACPI_COMPANION_SET(&spi->dev, adev);
  1425. spi->irq = -1;
  1426. INIT_LIST_HEAD(&resource_list);
  1427. ret = acpi_dev_get_resources(adev, &resource_list,
  1428. acpi_spi_add_resource, spi);
  1429. acpi_dev_free_resource_list(&resource_list);
  1430. if (ret < 0 || !spi->max_speed_hz) {
  1431. spi_dev_put(spi);
  1432. return AE_OK;
  1433. }
  1434. if (spi->irq < 0)
  1435. spi->irq = acpi_dev_gpio_irq_get(adev, 0);
  1436. adev->power.flags.ignore_parent = true;
  1437. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1438. if (spi_add_device(spi)) {
  1439. adev->power.flags.ignore_parent = false;
  1440. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1441. dev_name(&adev->dev));
  1442. spi_dev_put(spi);
  1443. }
  1444. return AE_OK;
  1445. }
  1446. static void acpi_register_spi_devices(struct spi_master *master)
  1447. {
  1448. acpi_status status;
  1449. acpi_handle handle;
  1450. handle = ACPI_HANDLE(master->dev.parent);
  1451. if (!handle)
  1452. return;
  1453. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1454. acpi_spi_add_device, NULL,
  1455. master, NULL);
  1456. if (ACPI_FAILURE(status))
  1457. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1458. }
  1459. #else
  1460. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1461. #endif /* CONFIG_ACPI */
  1462. static void spi_master_release(struct device *dev)
  1463. {
  1464. struct spi_master *master;
  1465. master = container_of(dev, struct spi_master, dev);
  1466. kfree(master);
  1467. }
  1468. static struct class spi_master_class = {
  1469. .name = "spi_master",
  1470. .owner = THIS_MODULE,
  1471. .dev_release = spi_master_release,
  1472. .dev_groups = spi_master_groups,
  1473. };
  1474. /**
  1475. * spi_alloc_master - allocate SPI master controller
  1476. * @dev: the controller, possibly using the platform_bus
  1477. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1478. * memory is in the driver_data field of the returned device,
  1479. * accessible with spi_master_get_devdata().
  1480. * Context: can sleep
  1481. *
  1482. * This call is used only by SPI master controller drivers, which are the
  1483. * only ones directly touching chip registers. It's how they allocate
  1484. * an spi_master structure, prior to calling spi_register_master().
  1485. *
  1486. * This must be called from context that can sleep.
  1487. *
  1488. * The caller is responsible for assigning the bus number and initializing
  1489. * the master's methods before calling spi_register_master(); and (after errors
  1490. * adding the device) calling spi_master_put() to prevent a memory leak.
  1491. *
  1492. * Return: the SPI master structure on success, else NULL.
  1493. */
  1494. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1495. {
  1496. struct spi_master *master;
  1497. if (!dev)
  1498. return NULL;
  1499. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1500. if (!master)
  1501. return NULL;
  1502. device_initialize(&master->dev);
  1503. master->bus_num = -1;
  1504. master->num_chipselect = 1;
  1505. master->dev.class = &spi_master_class;
  1506. master->dev.parent = dev;
  1507. pm_suspend_ignore_children(&master->dev, true);
  1508. spi_master_set_devdata(master, &master[1]);
  1509. return master;
  1510. }
  1511. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1512. #ifdef CONFIG_OF
  1513. static int of_spi_register_master(struct spi_master *master)
  1514. {
  1515. int nb, i, *cs;
  1516. struct device_node *np = master->dev.of_node;
  1517. if (!np)
  1518. return 0;
  1519. nb = of_gpio_named_count(np, "cs-gpios");
  1520. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1521. /* Return error only for an incorrectly formed cs-gpios property */
  1522. if (nb == 0 || nb == -ENOENT)
  1523. return 0;
  1524. else if (nb < 0)
  1525. return nb;
  1526. cs = devm_kzalloc(&master->dev,
  1527. sizeof(int) * master->num_chipselect,
  1528. GFP_KERNEL);
  1529. master->cs_gpios = cs;
  1530. if (!master->cs_gpios)
  1531. return -ENOMEM;
  1532. for (i = 0; i < master->num_chipselect; i++)
  1533. cs[i] = -ENOENT;
  1534. for (i = 0; i < nb; i++)
  1535. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1536. return 0;
  1537. }
  1538. #else
  1539. static int of_spi_register_master(struct spi_master *master)
  1540. {
  1541. return 0;
  1542. }
  1543. #endif
  1544. /**
  1545. * spi_register_master - register SPI master controller
  1546. * @master: initialized master, originally from spi_alloc_master()
  1547. * Context: can sleep
  1548. *
  1549. * SPI master controllers connect to their drivers using some non-SPI bus,
  1550. * such as the platform bus. The final stage of probe() in that code
  1551. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1552. *
  1553. * SPI controllers use board specific (often SOC specific) bus numbers,
  1554. * and board-specific addressing for SPI devices combines those numbers
  1555. * with chip select numbers. Since SPI does not directly support dynamic
  1556. * device identification, boards need configuration tables telling which
  1557. * chip is at which address.
  1558. *
  1559. * This must be called from context that can sleep. It returns zero on
  1560. * success, else a negative error code (dropping the master's refcount).
  1561. * After a successful return, the caller is responsible for calling
  1562. * spi_unregister_master().
  1563. *
  1564. * Return: zero on success, else a negative error code.
  1565. */
  1566. int spi_register_master(struct spi_master *master)
  1567. {
  1568. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1569. struct device *dev = master->dev.parent;
  1570. struct boardinfo *bi;
  1571. int status = -ENODEV;
  1572. int dynamic = 0;
  1573. if (!dev)
  1574. return -ENODEV;
  1575. status = of_spi_register_master(master);
  1576. if (status)
  1577. return status;
  1578. /* even if it's just one always-selected device, there must
  1579. * be at least one chipselect
  1580. */
  1581. if (master->num_chipselect == 0)
  1582. return -EINVAL;
  1583. if ((master->bus_num < 0) && master->dev.of_node)
  1584. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1585. /* convention: dynamically assigned bus IDs count down from the max */
  1586. if (master->bus_num < 0) {
  1587. /* FIXME switch to an IDR based scheme, something like
  1588. * I2C now uses, so we can't run out of "dynamic" IDs
  1589. */
  1590. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1591. dynamic = 1;
  1592. }
  1593. INIT_LIST_HEAD(&master->queue);
  1594. spin_lock_init(&master->queue_lock);
  1595. spin_lock_init(&master->bus_lock_spinlock);
  1596. mutex_init(&master->bus_lock_mutex);
  1597. mutex_init(&master->io_mutex);
  1598. master->bus_lock_flag = 0;
  1599. init_completion(&master->xfer_completion);
  1600. if (!master->max_dma_len)
  1601. master->max_dma_len = INT_MAX;
  1602. /* register the device, then userspace will see it.
  1603. * registration fails if the bus ID is in use.
  1604. */
  1605. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1606. status = device_add(&master->dev);
  1607. if (status < 0)
  1608. goto done;
  1609. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1610. dynamic ? " (dynamic)" : "");
  1611. /* If we're using a queued driver, start the queue */
  1612. if (master->transfer)
  1613. dev_info(dev, "master is unqueued, this is deprecated\n");
  1614. else {
  1615. status = spi_master_initialize_queue(master);
  1616. if (status) {
  1617. device_del(&master->dev);
  1618. goto done;
  1619. }
  1620. }
  1621. /* add statistics */
  1622. spin_lock_init(&master->statistics.lock);
  1623. mutex_lock(&board_lock);
  1624. list_add_tail(&master->list, &spi_master_list);
  1625. list_for_each_entry(bi, &board_list, list)
  1626. spi_match_master_to_boardinfo(master, &bi->board_info);
  1627. mutex_unlock(&board_lock);
  1628. /* Register devices from the device tree and ACPI */
  1629. of_register_spi_devices(master);
  1630. acpi_register_spi_devices(master);
  1631. done:
  1632. return status;
  1633. }
  1634. EXPORT_SYMBOL_GPL(spi_register_master);
  1635. static void devm_spi_unregister(struct device *dev, void *res)
  1636. {
  1637. spi_unregister_master(*(struct spi_master **)res);
  1638. }
  1639. /**
  1640. * dev_spi_register_master - register managed SPI master controller
  1641. * @dev: device managing SPI master
  1642. * @master: initialized master, originally from spi_alloc_master()
  1643. * Context: can sleep
  1644. *
  1645. * Register a SPI device as with spi_register_master() which will
  1646. * automatically be unregister
  1647. *
  1648. * Return: zero on success, else a negative error code.
  1649. */
  1650. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1651. {
  1652. struct spi_master **ptr;
  1653. int ret;
  1654. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1655. if (!ptr)
  1656. return -ENOMEM;
  1657. ret = spi_register_master(master);
  1658. if (!ret) {
  1659. *ptr = master;
  1660. devres_add(dev, ptr);
  1661. } else {
  1662. devres_free(ptr);
  1663. }
  1664. return ret;
  1665. }
  1666. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1667. static int __unregister(struct device *dev, void *null)
  1668. {
  1669. spi_unregister_device(to_spi_device(dev));
  1670. return 0;
  1671. }
  1672. /**
  1673. * spi_unregister_master - unregister SPI master controller
  1674. * @master: the master being unregistered
  1675. * Context: can sleep
  1676. *
  1677. * This call is used only by SPI master controller drivers, which are the
  1678. * only ones directly touching chip registers.
  1679. *
  1680. * This must be called from context that can sleep.
  1681. */
  1682. void spi_unregister_master(struct spi_master *master)
  1683. {
  1684. int dummy;
  1685. if (master->queued) {
  1686. if (spi_destroy_queue(master))
  1687. dev_err(&master->dev, "queue remove failed\n");
  1688. }
  1689. mutex_lock(&board_lock);
  1690. list_del(&master->list);
  1691. mutex_unlock(&board_lock);
  1692. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1693. device_unregister(&master->dev);
  1694. }
  1695. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1696. int spi_master_suspend(struct spi_master *master)
  1697. {
  1698. int ret;
  1699. /* Basically no-ops for non-queued masters */
  1700. if (!master->queued)
  1701. return 0;
  1702. ret = spi_stop_queue(master);
  1703. if (ret)
  1704. dev_err(&master->dev, "queue stop failed\n");
  1705. return ret;
  1706. }
  1707. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1708. int spi_master_resume(struct spi_master *master)
  1709. {
  1710. int ret;
  1711. if (!master->queued)
  1712. return 0;
  1713. ret = spi_start_queue(master);
  1714. if (ret)
  1715. dev_err(&master->dev, "queue restart failed\n");
  1716. return ret;
  1717. }
  1718. EXPORT_SYMBOL_GPL(spi_master_resume);
  1719. static int __spi_master_match(struct device *dev, const void *data)
  1720. {
  1721. struct spi_master *m;
  1722. const u16 *bus_num = data;
  1723. m = container_of(dev, struct spi_master, dev);
  1724. return m->bus_num == *bus_num;
  1725. }
  1726. /**
  1727. * spi_busnum_to_master - look up master associated with bus_num
  1728. * @bus_num: the master's bus number
  1729. * Context: can sleep
  1730. *
  1731. * This call may be used with devices that are registered after
  1732. * arch init time. It returns a refcounted pointer to the relevant
  1733. * spi_master (which the caller must release), or NULL if there is
  1734. * no such master registered.
  1735. *
  1736. * Return: the SPI master structure on success, else NULL.
  1737. */
  1738. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1739. {
  1740. struct device *dev;
  1741. struct spi_master *master = NULL;
  1742. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1743. __spi_master_match);
  1744. if (dev)
  1745. master = container_of(dev, struct spi_master, dev);
  1746. /* reference got in class_find_device */
  1747. return master;
  1748. }
  1749. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1750. /*-------------------------------------------------------------------------*/
  1751. /* Core methods for SPI resource management */
  1752. /**
  1753. * spi_res_alloc - allocate a spi resource that is life-cycle managed
  1754. * during the processing of a spi_message while using
  1755. * spi_transfer_one
  1756. * @spi: the spi device for which we allocate memory
  1757. * @release: the release code to execute for this resource
  1758. * @size: size to alloc and return
  1759. * @gfp: GFP allocation flags
  1760. *
  1761. * Return: the pointer to the allocated data
  1762. *
  1763. * This may get enhanced in the future to allocate from a memory pool
  1764. * of the @spi_device or @spi_master to avoid repeated allocations.
  1765. */
  1766. void *spi_res_alloc(struct spi_device *spi,
  1767. spi_res_release_t release,
  1768. size_t size, gfp_t gfp)
  1769. {
  1770. struct spi_res *sres;
  1771. sres = kzalloc(sizeof(*sres) + size, gfp);
  1772. if (!sres)
  1773. return NULL;
  1774. INIT_LIST_HEAD(&sres->entry);
  1775. sres->release = release;
  1776. return sres->data;
  1777. }
  1778. EXPORT_SYMBOL_GPL(spi_res_alloc);
  1779. /**
  1780. * spi_res_free - free an spi resource
  1781. * @res: pointer to the custom data of a resource
  1782. *
  1783. */
  1784. void spi_res_free(void *res)
  1785. {
  1786. struct spi_res *sres = container_of(res, struct spi_res, data);
  1787. if (!res)
  1788. return;
  1789. WARN_ON(!list_empty(&sres->entry));
  1790. kfree(sres);
  1791. }
  1792. EXPORT_SYMBOL_GPL(spi_res_free);
  1793. /**
  1794. * spi_res_add - add a spi_res to the spi_message
  1795. * @message: the spi message
  1796. * @res: the spi_resource
  1797. */
  1798. void spi_res_add(struct spi_message *message, void *res)
  1799. {
  1800. struct spi_res *sres = container_of(res, struct spi_res, data);
  1801. WARN_ON(!list_empty(&sres->entry));
  1802. list_add_tail(&sres->entry, &message->resources);
  1803. }
  1804. EXPORT_SYMBOL_GPL(spi_res_add);
  1805. /**
  1806. * spi_res_release - release all spi resources for this message
  1807. * @master: the @spi_master
  1808. * @message: the @spi_message
  1809. */
  1810. void spi_res_release(struct spi_master *master,
  1811. struct spi_message *message)
  1812. {
  1813. struct spi_res *res;
  1814. while (!list_empty(&message->resources)) {
  1815. res = list_last_entry(&message->resources,
  1816. struct spi_res, entry);
  1817. if (res->release)
  1818. res->release(master, message, res->data);
  1819. list_del(&res->entry);
  1820. kfree(res);
  1821. }
  1822. }
  1823. EXPORT_SYMBOL_GPL(spi_res_release);
  1824. /*-------------------------------------------------------------------------*/
  1825. /* Core methods for spi_message alterations */
  1826. static void __spi_replace_transfers_release(struct spi_master *master,
  1827. struct spi_message *msg,
  1828. void *res)
  1829. {
  1830. struct spi_replaced_transfers *rxfer = res;
  1831. size_t i;
  1832. /* call extra callback if requested */
  1833. if (rxfer->release)
  1834. rxfer->release(master, msg, res);
  1835. /* insert replaced transfers back into the message */
  1836. list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
  1837. /* remove the formerly inserted entries */
  1838. for (i = 0; i < rxfer->inserted; i++)
  1839. list_del(&rxfer->inserted_transfers[i].transfer_list);
  1840. }
  1841. /**
  1842. * spi_replace_transfers - replace transfers with several transfers
  1843. * and register change with spi_message.resources
  1844. * @msg: the spi_message we work upon
  1845. * @xfer_first: the first spi_transfer we want to replace
  1846. * @remove: number of transfers to remove
  1847. * @insert: the number of transfers we want to insert instead
  1848. * @release: extra release code necessary in some circumstances
  1849. * @extradatasize: extra data to allocate (with alignment guarantees
  1850. * of struct @spi_transfer)
  1851. * @gfp: gfp flags
  1852. *
  1853. * Returns: pointer to @spi_replaced_transfers,
  1854. * PTR_ERR(...) in case of errors.
  1855. */
  1856. struct spi_replaced_transfers *spi_replace_transfers(
  1857. struct spi_message *msg,
  1858. struct spi_transfer *xfer_first,
  1859. size_t remove,
  1860. size_t insert,
  1861. spi_replaced_release_t release,
  1862. size_t extradatasize,
  1863. gfp_t gfp)
  1864. {
  1865. struct spi_replaced_transfers *rxfer;
  1866. struct spi_transfer *xfer;
  1867. size_t i;
  1868. /* allocate the structure using spi_res */
  1869. rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
  1870. insert * sizeof(struct spi_transfer)
  1871. + sizeof(struct spi_replaced_transfers)
  1872. + extradatasize,
  1873. gfp);
  1874. if (!rxfer)
  1875. return ERR_PTR(-ENOMEM);
  1876. /* the release code to invoke before running the generic release */
  1877. rxfer->release = release;
  1878. /* assign extradata */
  1879. if (extradatasize)
  1880. rxfer->extradata =
  1881. &rxfer->inserted_transfers[insert];
  1882. /* init the replaced_transfers list */
  1883. INIT_LIST_HEAD(&rxfer->replaced_transfers);
  1884. /* assign the list_entry after which we should reinsert
  1885. * the @replaced_transfers - it may be spi_message.messages!
  1886. */
  1887. rxfer->replaced_after = xfer_first->transfer_list.prev;
  1888. /* remove the requested number of transfers */
  1889. for (i = 0; i < remove; i++) {
  1890. /* if the entry after replaced_after it is msg->transfers
  1891. * then we have been requested to remove more transfers
  1892. * than are in the list
  1893. */
  1894. if (rxfer->replaced_after->next == &msg->transfers) {
  1895. dev_err(&msg->spi->dev,
  1896. "requested to remove more spi_transfers than are available\n");
  1897. /* insert replaced transfers back into the message */
  1898. list_splice(&rxfer->replaced_transfers,
  1899. rxfer->replaced_after);
  1900. /* free the spi_replace_transfer structure */
  1901. spi_res_free(rxfer);
  1902. /* and return with an error */
  1903. return ERR_PTR(-EINVAL);
  1904. }
  1905. /* remove the entry after replaced_after from list of
  1906. * transfers and add it to list of replaced_transfers
  1907. */
  1908. list_move_tail(rxfer->replaced_after->next,
  1909. &rxfer->replaced_transfers);
  1910. }
  1911. /* create copy of the given xfer with identical settings
  1912. * based on the first transfer to get removed
  1913. */
  1914. for (i = 0; i < insert; i++) {
  1915. /* we need to run in reverse order */
  1916. xfer = &rxfer->inserted_transfers[insert - 1 - i];
  1917. /* copy all spi_transfer data */
  1918. memcpy(xfer, xfer_first, sizeof(*xfer));
  1919. /* add to list */
  1920. list_add(&xfer->transfer_list, rxfer->replaced_after);
  1921. /* clear cs_change and delay_usecs for all but the last */
  1922. if (i) {
  1923. xfer->cs_change = false;
  1924. xfer->delay_usecs = 0;
  1925. }
  1926. }
  1927. /* set up inserted */
  1928. rxfer->inserted = insert;
  1929. /* and register it with spi_res/spi_message */
  1930. spi_res_add(msg, rxfer);
  1931. return rxfer;
  1932. }
  1933. EXPORT_SYMBOL_GPL(spi_replace_transfers);
  1934. static int __spi_split_transfer_maxsize(struct spi_master *master,
  1935. struct spi_message *msg,
  1936. struct spi_transfer **xferp,
  1937. size_t maxsize,
  1938. gfp_t gfp)
  1939. {
  1940. struct spi_transfer *xfer = *xferp, *xfers;
  1941. struct spi_replaced_transfers *srt;
  1942. size_t offset;
  1943. size_t count, i;
  1944. /* warn once about this fact that we are splitting a transfer */
  1945. dev_warn_once(&msg->spi->dev,
  1946. "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
  1947. xfer->len, maxsize);
  1948. /* calculate how many we have to replace */
  1949. count = DIV_ROUND_UP(xfer->len, maxsize);
  1950. /* create replacement */
  1951. srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
  1952. if (IS_ERR(srt))
  1953. return PTR_ERR(srt);
  1954. xfers = srt->inserted_transfers;
  1955. /* now handle each of those newly inserted spi_transfers
  1956. * note that the replacements spi_transfers all are preset
  1957. * to the same values as *xferp, so tx_buf, rx_buf and len
  1958. * are all identical (as well as most others)
  1959. * so we just have to fix up len and the pointers.
  1960. *
  1961. * this also includes support for the depreciated
  1962. * spi_message.is_dma_mapped interface
  1963. */
  1964. /* the first transfer just needs the length modified, so we
  1965. * run it outside the loop
  1966. */
  1967. xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
  1968. /* all the others need rx_buf/tx_buf also set */
  1969. for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
  1970. /* update rx_buf, tx_buf and dma */
  1971. if (xfers[i].rx_buf)
  1972. xfers[i].rx_buf += offset;
  1973. if (xfers[i].rx_dma)
  1974. xfers[i].rx_dma += offset;
  1975. if (xfers[i].tx_buf)
  1976. xfers[i].tx_buf += offset;
  1977. if (xfers[i].tx_dma)
  1978. xfers[i].tx_dma += offset;
  1979. /* update length */
  1980. xfers[i].len = min(maxsize, xfers[i].len - offset);
  1981. }
  1982. /* we set up xferp to the last entry we have inserted,
  1983. * so that we skip those already split transfers
  1984. */
  1985. *xferp = &xfers[count - 1];
  1986. /* increment statistics counters */
  1987. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
  1988. transfers_split_maxsize);
  1989. SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
  1990. transfers_split_maxsize);
  1991. return 0;
  1992. }
  1993. /**
  1994. * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
  1995. * when an individual transfer exceeds a
  1996. * certain size
  1997. * @master: the @spi_master for this transfer
  1998. * @msg: the @spi_message to transform
  1999. * @maxsize: the maximum when to apply this
  2000. * @gfp: GFP allocation flags
  2001. *
  2002. * Return: status of transformation
  2003. */
  2004. int spi_split_transfers_maxsize(struct spi_master *master,
  2005. struct spi_message *msg,
  2006. size_t maxsize,
  2007. gfp_t gfp)
  2008. {
  2009. struct spi_transfer *xfer;
  2010. int ret;
  2011. /* iterate over the transfer_list,
  2012. * but note that xfer is advanced to the last transfer inserted
  2013. * to avoid checking sizes again unnecessarily (also xfer does
  2014. * potentiall belong to a different list by the time the
  2015. * replacement has happened
  2016. */
  2017. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  2018. if (xfer->len > maxsize) {
  2019. ret = __spi_split_transfer_maxsize(
  2020. master, msg, &xfer, maxsize, gfp);
  2021. if (ret)
  2022. return ret;
  2023. }
  2024. }
  2025. return 0;
  2026. }
  2027. EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
  2028. /*-------------------------------------------------------------------------*/
  2029. /* Core methods for SPI master protocol drivers. Some of the
  2030. * other core methods are currently defined as inline functions.
  2031. */
  2032. static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
  2033. {
  2034. if (master->bits_per_word_mask) {
  2035. /* Only 32 bits fit in the mask */
  2036. if (bits_per_word > 32)
  2037. return -EINVAL;
  2038. if (!(master->bits_per_word_mask &
  2039. SPI_BPW_MASK(bits_per_word)))
  2040. return -EINVAL;
  2041. }
  2042. return 0;
  2043. }
  2044. /**
  2045. * spi_setup - setup SPI mode and clock rate
  2046. * @spi: the device whose settings are being modified
  2047. * Context: can sleep, and no requests are queued to the device
  2048. *
  2049. * SPI protocol drivers may need to update the transfer mode if the
  2050. * device doesn't work with its default. They may likewise need
  2051. * to update clock rates or word sizes from initial values. This function
  2052. * changes those settings, and must be called from a context that can sleep.
  2053. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  2054. * effect the next time the device is selected and data is transferred to
  2055. * or from it. When this function returns, the spi device is deselected.
  2056. *
  2057. * Note that this call will fail if the protocol driver specifies an option
  2058. * that the underlying controller or its driver does not support. For
  2059. * example, not all hardware supports wire transfers using nine bit words,
  2060. * LSB-first wire encoding, or active-high chipselects.
  2061. *
  2062. * Return: zero on success, else a negative error code.
  2063. */
  2064. int spi_setup(struct spi_device *spi)
  2065. {
  2066. unsigned bad_bits, ugly_bits;
  2067. int status;
  2068. /* check mode to prevent that DUAL and QUAD set at the same time
  2069. */
  2070. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  2071. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  2072. dev_err(&spi->dev,
  2073. "setup: can not select dual and quad at the same time\n");
  2074. return -EINVAL;
  2075. }
  2076. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  2077. */
  2078. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  2079. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  2080. return -EINVAL;
  2081. /* help drivers fail *cleanly* when they need options
  2082. * that aren't supported with their current master
  2083. */
  2084. bad_bits = spi->mode & ~spi->master->mode_bits;
  2085. ugly_bits = bad_bits &
  2086. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  2087. if (ugly_bits) {
  2088. dev_warn(&spi->dev,
  2089. "setup: ignoring unsupported mode bits %x\n",
  2090. ugly_bits);
  2091. spi->mode &= ~ugly_bits;
  2092. bad_bits &= ~ugly_bits;
  2093. }
  2094. if (bad_bits) {
  2095. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  2096. bad_bits);
  2097. return -EINVAL;
  2098. }
  2099. if (!spi->bits_per_word)
  2100. spi->bits_per_word = 8;
  2101. status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
  2102. if (status)
  2103. return status;
  2104. if (!spi->max_speed_hz)
  2105. spi->max_speed_hz = spi->master->max_speed_hz;
  2106. if (spi->master->setup)
  2107. status = spi->master->setup(spi);
  2108. spi_set_cs(spi, false);
  2109. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  2110. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  2111. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  2112. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  2113. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  2114. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  2115. spi->bits_per_word, spi->max_speed_hz,
  2116. status);
  2117. return status;
  2118. }
  2119. EXPORT_SYMBOL_GPL(spi_setup);
  2120. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  2121. {
  2122. struct spi_master *master = spi->master;
  2123. struct spi_transfer *xfer;
  2124. int w_size;
  2125. if (list_empty(&message->transfers))
  2126. return -EINVAL;
  2127. /* Half-duplex links include original MicroWire, and ones with
  2128. * only one data pin like SPI_3WIRE (switches direction) or where
  2129. * either MOSI or MISO is missing. They can also be caused by
  2130. * software limitations.
  2131. */
  2132. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  2133. || (spi->mode & SPI_3WIRE)) {
  2134. unsigned flags = master->flags;
  2135. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  2136. if (xfer->rx_buf && xfer->tx_buf)
  2137. return -EINVAL;
  2138. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  2139. return -EINVAL;
  2140. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  2141. return -EINVAL;
  2142. }
  2143. }
  2144. /**
  2145. * Set transfer bits_per_word and max speed as spi device default if
  2146. * it is not set for this transfer.
  2147. * Set transfer tx_nbits and rx_nbits as single transfer default
  2148. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  2149. */
  2150. message->frame_length = 0;
  2151. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  2152. message->frame_length += xfer->len;
  2153. if (!xfer->bits_per_word)
  2154. xfer->bits_per_word = spi->bits_per_word;
  2155. if (!xfer->speed_hz)
  2156. xfer->speed_hz = spi->max_speed_hz;
  2157. if (!xfer->speed_hz)
  2158. xfer->speed_hz = master->max_speed_hz;
  2159. if (master->max_speed_hz &&
  2160. xfer->speed_hz > master->max_speed_hz)
  2161. xfer->speed_hz = master->max_speed_hz;
  2162. if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
  2163. return -EINVAL;
  2164. /*
  2165. * SPI transfer length should be multiple of SPI word size
  2166. * where SPI word size should be power-of-two multiple
  2167. */
  2168. if (xfer->bits_per_word <= 8)
  2169. w_size = 1;
  2170. else if (xfer->bits_per_word <= 16)
  2171. w_size = 2;
  2172. else
  2173. w_size = 4;
  2174. /* No partial transfers accepted */
  2175. if (xfer->len % w_size)
  2176. return -EINVAL;
  2177. if (xfer->speed_hz && master->min_speed_hz &&
  2178. xfer->speed_hz < master->min_speed_hz)
  2179. return -EINVAL;
  2180. if (xfer->tx_buf && !xfer->tx_nbits)
  2181. xfer->tx_nbits = SPI_NBITS_SINGLE;
  2182. if (xfer->rx_buf && !xfer->rx_nbits)
  2183. xfer->rx_nbits = SPI_NBITS_SINGLE;
  2184. /* check transfer tx/rx_nbits:
  2185. * 1. check the value matches one of single, dual and quad
  2186. * 2. check tx/rx_nbits match the mode in spi_device
  2187. */
  2188. if (xfer->tx_buf) {
  2189. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  2190. xfer->tx_nbits != SPI_NBITS_DUAL &&
  2191. xfer->tx_nbits != SPI_NBITS_QUAD)
  2192. return -EINVAL;
  2193. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  2194. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  2195. return -EINVAL;
  2196. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  2197. !(spi->mode & SPI_TX_QUAD))
  2198. return -EINVAL;
  2199. }
  2200. /* check transfer rx_nbits */
  2201. if (xfer->rx_buf) {
  2202. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  2203. xfer->rx_nbits != SPI_NBITS_DUAL &&
  2204. xfer->rx_nbits != SPI_NBITS_QUAD)
  2205. return -EINVAL;
  2206. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  2207. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  2208. return -EINVAL;
  2209. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  2210. !(spi->mode & SPI_RX_QUAD))
  2211. return -EINVAL;
  2212. }
  2213. }
  2214. message->status = -EINPROGRESS;
  2215. return 0;
  2216. }
  2217. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  2218. {
  2219. struct spi_master *master = spi->master;
  2220. message->spi = spi;
  2221. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
  2222. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
  2223. trace_spi_message_submit(message);
  2224. return master->transfer(spi, message);
  2225. }
  2226. /**
  2227. * spi_async - asynchronous SPI transfer
  2228. * @spi: device with which data will be exchanged
  2229. * @message: describes the data transfers, including completion callback
  2230. * Context: any (irqs may be blocked, etc)
  2231. *
  2232. * This call may be used in_irq and other contexts which can't sleep,
  2233. * as well as from task contexts which can sleep.
  2234. *
  2235. * The completion callback is invoked in a context which can't sleep.
  2236. * Before that invocation, the value of message->status is undefined.
  2237. * When the callback is issued, message->status holds either zero (to
  2238. * indicate complete success) or a negative error code. After that
  2239. * callback returns, the driver which issued the transfer request may
  2240. * deallocate the associated memory; it's no longer in use by any SPI
  2241. * core or controller driver code.
  2242. *
  2243. * Note that although all messages to a spi_device are handled in
  2244. * FIFO order, messages may go to different devices in other orders.
  2245. * Some device might be higher priority, or have various "hard" access
  2246. * time requirements, for example.
  2247. *
  2248. * On detection of any fault during the transfer, processing of
  2249. * the entire message is aborted, and the device is deselected.
  2250. * Until returning from the associated message completion callback,
  2251. * no other spi_message queued to that device will be processed.
  2252. * (This rule applies equally to all the synchronous transfer calls,
  2253. * which are wrappers around this core asynchronous primitive.)
  2254. *
  2255. * Return: zero on success, else a negative error code.
  2256. */
  2257. int spi_async(struct spi_device *spi, struct spi_message *message)
  2258. {
  2259. struct spi_master *master = spi->master;
  2260. int ret;
  2261. unsigned long flags;
  2262. ret = __spi_validate(spi, message);
  2263. if (ret != 0)
  2264. return ret;
  2265. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2266. if (master->bus_lock_flag)
  2267. ret = -EBUSY;
  2268. else
  2269. ret = __spi_async(spi, message);
  2270. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2271. return ret;
  2272. }
  2273. EXPORT_SYMBOL_GPL(spi_async);
  2274. /**
  2275. * spi_async_locked - version of spi_async with exclusive bus usage
  2276. * @spi: device with which data will be exchanged
  2277. * @message: describes the data transfers, including completion callback
  2278. * Context: any (irqs may be blocked, etc)
  2279. *
  2280. * This call may be used in_irq and other contexts which can't sleep,
  2281. * as well as from task contexts which can sleep.
  2282. *
  2283. * The completion callback is invoked in a context which can't sleep.
  2284. * Before that invocation, the value of message->status is undefined.
  2285. * When the callback is issued, message->status holds either zero (to
  2286. * indicate complete success) or a negative error code. After that
  2287. * callback returns, the driver which issued the transfer request may
  2288. * deallocate the associated memory; it's no longer in use by any SPI
  2289. * core or controller driver code.
  2290. *
  2291. * Note that although all messages to a spi_device are handled in
  2292. * FIFO order, messages may go to different devices in other orders.
  2293. * Some device might be higher priority, or have various "hard" access
  2294. * time requirements, for example.
  2295. *
  2296. * On detection of any fault during the transfer, processing of
  2297. * the entire message is aborted, and the device is deselected.
  2298. * Until returning from the associated message completion callback,
  2299. * no other spi_message queued to that device will be processed.
  2300. * (This rule applies equally to all the synchronous transfer calls,
  2301. * which are wrappers around this core asynchronous primitive.)
  2302. *
  2303. * Return: zero on success, else a negative error code.
  2304. */
  2305. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  2306. {
  2307. struct spi_master *master = spi->master;
  2308. int ret;
  2309. unsigned long flags;
  2310. ret = __spi_validate(spi, message);
  2311. if (ret != 0)
  2312. return ret;
  2313. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2314. ret = __spi_async(spi, message);
  2315. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2316. return ret;
  2317. }
  2318. EXPORT_SYMBOL_GPL(spi_async_locked);
  2319. int spi_flash_read(struct spi_device *spi,
  2320. struct spi_flash_read_message *msg)
  2321. {
  2322. struct spi_master *master = spi->master;
  2323. struct device *rx_dev = NULL;
  2324. int ret;
  2325. if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
  2326. msg->addr_nbits == SPI_NBITS_DUAL) &&
  2327. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  2328. return -EINVAL;
  2329. if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
  2330. msg->addr_nbits == SPI_NBITS_QUAD) &&
  2331. !(spi->mode & SPI_TX_QUAD))
  2332. return -EINVAL;
  2333. if (msg->data_nbits == SPI_NBITS_DUAL &&
  2334. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  2335. return -EINVAL;
  2336. if (msg->data_nbits == SPI_NBITS_QUAD &&
  2337. !(spi->mode & SPI_RX_QUAD))
  2338. return -EINVAL;
  2339. if (master->auto_runtime_pm) {
  2340. ret = pm_runtime_get_sync(master->dev.parent);
  2341. if (ret < 0) {
  2342. dev_err(&master->dev, "Failed to power device: %d\n",
  2343. ret);
  2344. return ret;
  2345. }
  2346. }
  2347. mutex_lock(&master->bus_lock_mutex);
  2348. mutex_lock(&master->io_mutex);
  2349. if (master->dma_rx) {
  2350. rx_dev = master->dma_rx->device->dev;
  2351. ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
  2352. msg->buf, msg->len,
  2353. DMA_FROM_DEVICE);
  2354. if (!ret)
  2355. msg->cur_msg_mapped = true;
  2356. }
  2357. ret = master->spi_flash_read(spi, msg);
  2358. if (msg->cur_msg_mapped)
  2359. spi_unmap_buf(master, rx_dev, &msg->rx_sg,
  2360. DMA_FROM_DEVICE);
  2361. mutex_unlock(&master->io_mutex);
  2362. mutex_unlock(&master->bus_lock_mutex);
  2363. if (master->auto_runtime_pm)
  2364. pm_runtime_put(master->dev.parent);
  2365. return ret;
  2366. }
  2367. EXPORT_SYMBOL_GPL(spi_flash_read);
  2368. /*-------------------------------------------------------------------------*/
  2369. /* Utility methods for SPI master protocol drivers, layered on
  2370. * top of the core. Some other utility methods are defined as
  2371. * inline functions.
  2372. */
  2373. static void spi_complete(void *arg)
  2374. {
  2375. complete(arg);
  2376. }
  2377. static int __spi_sync(struct spi_device *spi, struct spi_message *message)
  2378. {
  2379. DECLARE_COMPLETION_ONSTACK(done);
  2380. int status;
  2381. struct spi_master *master = spi->master;
  2382. unsigned long flags;
  2383. status = __spi_validate(spi, message);
  2384. if (status != 0)
  2385. return status;
  2386. message->complete = spi_complete;
  2387. message->context = &done;
  2388. message->spi = spi;
  2389. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
  2390. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
  2391. /* If we're not using the legacy transfer method then we will
  2392. * try to transfer in the calling context so special case.
  2393. * This code would be less tricky if we could remove the
  2394. * support for driver implemented message queues.
  2395. */
  2396. if (master->transfer == spi_queued_transfer) {
  2397. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2398. trace_spi_message_submit(message);
  2399. status = __spi_queued_transfer(spi, message, false);
  2400. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2401. } else {
  2402. status = spi_async_locked(spi, message);
  2403. }
  2404. if (status == 0) {
  2405. /* Push out the messages in the calling context if we
  2406. * can.
  2407. */
  2408. if (master->transfer == spi_queued_transfer) {
  2409. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
  2410. spi_sync_immediate);
  2411. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
  2412. spi_sync_immediate);
  2413. __spi_pump_messages(master, false);
  2414. }
  2415. wait_for_completion(&done);
  2416. status = message->status;
  2417. }
  2418. message->context = NULL;
  2419. return status;
  2420. }
  2421. /**
  2422. * spi_sync - blocking/synchronous SPI data transfers
  2423. * @spi: device with which data will be exchanged
  2424. * @message: describes the data transfers
  2425. * Context: can sleep
  2426. *
  2427. * This call may only be used from a context that may sleep. The sleep
  2428. * is non-interruptible, and has no timeout. Low-overhead controller
  2429. * drivers may DMA directly into and out of the message buffers.
  2430. *
  2431. * Note that the SPI device's chip select is active during the message,
  2432. * and then is normally disabled between messages. Drivers for some
  2433. * frequently-used devices may want to minimize costs of selecting a chip,
  2434. * by leaving it selected in anticipation that the next message will go
  2435. * to the same chip. (That may increase power usage.)
  2436. *
  2437. * Also, the caller is guaranteeing that the memory associated with the
  2438. * message will not be freed before this call returns.
  2439. *
  2440. * Return: zero on success, else a negative error code.
  2441. */
  2442. int spi_sync(struct spi_device *spi, struct spi_message *message)
  2443. {
  2444. int ret;
  2445. mutex_lock(&spi->master->bus_lock_mutex);
  2446. ret = __spi_sync(spi, message);
  2447. mutex_unlock(&spi->master->bus_lock_mutex);
  2448. return ret;
  2449. }
  2450. EXPORT_SYMBOL_GPL(spi_sync);
  2451. /**
  2452. * spi_sync_locked - version of spi_sync with exclusive bus usage
  2453. * @spi: device with which data will be exchanged
  2454. * @message: describes the data transfers
  2455. * Context: can sleep
  2456. *
  2457. * This call may only be used from a context that may sleep. The sleep
  2458. * is non-interruptible, and has no timeout. Low-overhead controller
  2459. * drivers may DMA directly into and out of the message buffers.
  2460. *
  2461. * This call should be used by drivers that require exclusive access to the
  2462. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  2463. * be released by a spi_bus_unlock call when the exclusive access is over.
  2464. *
  2465. * Return: zero on success, else a negative error code.
  2466. */
  2467. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  2468. {
  2469. return __spi_sync(spi, message);
  2470. }
  2471. EXPORT_SYMBOL_GPL(spi_sync_locked);
  2472. /**
  2473. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  2474. * @master: SPI bus master that should be locked for exclusive bus access
  2475. * Context: can sleep
  2476. *
  2477. * This call may only be used from a context that may sleep. The sleep
  2478. * is non-interruptible, and has no timeout.
  2479. *
  2480. * This call should be used by drivers that require exclusive access to the
  2481. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  2482. * exclusive access is over. Data transfer must be done by spi_sync_locked
  2483. * and spi_async_locked calls when the SPI bus lock is held.
  2484. *
  2485. * Return: always zero.
  2486. */
  2487. int spi_bus_lock(struct spi_master *master)
  2488. {
  2489. unsigned long flags;
  2490. mutex_lock(&master->bus_lock_mutex);
  2491. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2492. master->bus_lock_flag = 1;
  2493. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2494. /* mutex remains locked until spi_bus_unlock is called */
  2495. return 0;
  2496. }
  2497. EXPORT_SYMBOL_GPL(spi_bus_lock);
  2498. /**
  2499. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  2500. * @master: SPI bus master that was locked for exclusive bus access
  2501. * Context: can sleep
  2502. *
  2503. * This call may only be used from a context that may sleep. The sleep
  2504. * is non-interruptible, and has no timeout.
  2505. *
  2506. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  2507. * call.
  2508. *
  2509. * Return: always zero.
  2510. */
  2511. int spi_bus_unlock(struct spi_master *master)
  2512. {
  2513. master->bus_lock_flag = 0;
  2514. mutex_unlock(&master->bus_lock_mutex);
  2515. return 0;
  2516. }
  2517. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  2518. /* portable code must never pass more than 32 bytes */
  2519. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  2520. static u8 *buf;
  2521. /**
  2522. * spi_write_then_read - SPI synchronous write followed by read
  2523. * @spi: device with which data will be exchanged
  2524. * @txbuf: data to be written (need not be dma-safe)
  2525. * @n_tx: size of txbuf, in bytes
  2526. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  2527. * @n_rx: size of rxbuf, in bytes
  2528. * Context: can sleep
  2529. *
  2530. * This performs a half duplex MicroWire style transaction with the
  2531. * device, sending txbuf and then reading rxbuf. The return value
  2532. * is zero for success, else a negative errno status code.
  2533. * This call may only be used from a context that may sleep.
  2534. *
  2535. * Parameters to this routine are always copied using a small buffer;
  2536. * portable code should never use this for more than 32 bytes.
  2537. * Performance-sensitive or bulk transfer code should instead use
  2538. * spi_{async,sync}() calls with dma-safe buffers.
  2539. *
  2540. * Return: zero on success, else a negative error code.
  2541. */
  2542. int spi_write_then_read(struct spi_device *spi,
  2543. const void *txbuf, unsigned n_tx,
  2544. void *rxbuf, unsigned n_rx)
  2545. {
  2546. static DEFINE_MUTEX(lock);
  2547. int status;
  2548. struct spi_message message;
  2549. struct spi_transfer x[2];
  2550. u8 *local_buf;
  2551. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  2552. * copying here, (as a pure convenience thing), but we can
  2553. * keep heap costs out of the hot path unless someone else is
  2554. * using the pre-allocated buffer or the transfer is too large.
  2555. */
  2556. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  2557. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  2558. GFP_KERNEL | GFP_DMA);
  2559. if (!local_buf)
  2560. return -ENOMEM;
  2561. } else {
  2562. local_buf = buf;
  2563. }
  2564. spi_message_init(&message);
  2565. memset(x, 0, sizeof(x));
  2566. if (n_tx) {
  2567. x[0].len = n_tx;
  2568. spi_message_add_tail(&x[0], &message);
  2569. }
  2570. if (n_rx) {
  2571. x[1].len = n_rx;
  2572. spi_message_add_tail(&x[1], &message);
  2573. }
  2574. memcpy(local_buf, txbuf, n_tx);
  2575. x[0].tx_buf = local_buf;
  2576. x[1].rx_buf = local_buf + n_tx;
  2577. /* do the i/o */
  2578. status = spi_sync(spi, &message);
  2579. if (status == 0)
  2580. memcpy(rxbuf, x[1].rx_buf, n_rx);
  2581. if (x[0].tx_buf == buf)
  2582. mutex_unlock(&lock);
  2583. else
  2584. kfree(local_buf);
  2585. return status;
  2586. }
  2587. EXPORT_SYMBOL_GPL(spi_write_then_read);
  2588. /*-------------------------------------------------------------------------*/
  2589. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  2590. static int __spi_of_device_match(struct device *dev, void *data)
  2591. {
  2592. return dev->of_node == data;
  2593. }
  2594. /* must call put_device() when done with returned spi_device device */
  2595. static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
  2596. {
  2597. struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
  2598. __spi_of_device_match);
  2599. return dev ? to_spi_device(dev) : NULL;
  2600. }
  2601. static int __spi_of_master_match(struct device *dev, const void *data)
  2602. {
  2603. return dev->of_node == data;
  2604. }
  2605. /* the spi masters are not using spi_bus, so we find it with another way */
  2606. static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
  2607. {
  2608. struct device *dev;
  2609. dev = class_find_device(&spi_master_class, NULL, node,
  2610. __spi_of_master_match);
  2611. if (!dev)
  2612. return NULL;
  2613. /* reference got in class_find_device */
  2614. return container_of(dev, struct spi_master, dev);
  2615. }
  2616. static int of_spi_notify(struct notifier_block *nb, unsigned long action,
  2617. void *arg)
  2618. {
  2619. struct of_reconfig_data *rd = arg;
  2620. struct spi_master *master;
  2621. struct spi_device *spi;
  2622. switch (of_reconfig_get_state_change(action, arg)) {
  2623. case OF_RECONFIG_CHANGE_ADD:
  2624. master = of_find_spi_master_by_node(rd->dn->parent);
  2625. if (master == NULL)
  2626. return NOTIFY_OK; /* not for us */
  2627. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  2628. put_device(&master->dev);
  2629. return NOTIFY_OK;
  2630. }
  2631. spi = of_register_spi_device(master, rd->dn);
  2632. put_device(&master->dev);
  2633. if (IS_ERR(spi)) {
  2634. pr_err("%s: failed to create for '%s'\n",
  2635. __func__, rd->dn->full_name);
  2636. return notifier_from_errno(PTR_ERR(spi));
  2637. }
  2638. break;
  2639. case OF_RECONFIG_CHANGE_REMOVE:
  2640. /* already depopulated? */
  2641. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  2642. return NOTIFY_OK;
  2643. /* find our device by node */
  2644. spi = of_find_spi_device_by_node(rd->dn);
  2645. if (spi == NULL)
  2646. return NOTIFY_OK; /* no? not meant for us */
  2647. /* unregister takes one ref away */
  2648. spi_unregister_device(spi);
  2649. /* and put the reference of the find */
  2650. put_device(&spi->dev);
  2651. break;
  2652. }
  2653. return NOTIFY_OK;
  2654. }
  2655. static struct notifier_block spi_of_notifier = {
  2656. .notifier_call = of_spi_notify,
  2657. };
  2658. #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2659. extern struct notifier_block spi_of_notifier;
  2660. #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2661. static int __init spi_init(void)
  2662. {
  2663. int status;
  2664. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  2665. if (!buf) {
  2666. status = -ENOMEM;
  2667. goto err0;
  2668. }
  2669. status = bus_register(&spi_bus_type);
  2670. if (status < 0)
  2671. goto err1;
  2672. status = class_register(&spi_master_class);
  2673. if (status < 0)
  2674. goto err2;
  2675. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  2676. WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
  2677. return 0;
  2678. err2:
  2679. bus_unregister(&spi_bus_type);
  2680. err1:
  2681. kfree(buf);
  2682. buf = NULL;
  2683. err0:
  2684. return status;
  2685. }
  2686. /* board_info is normally registered in arch_initcall(),
  2687. * but even essential drivers wait till later
  2688. *
  2689. * REVISIT only boardinfo really needs static linking. the rest (device and
  2690. * driver registration) _could_ be dynamically linked (modular) ... costs
  2691. * include needing to have boardinfo data structures be much more public.
  2692. */
  2693. postcore_initcall(spi_init);