spi.c 88 KB

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