spi.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293
  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. of_node_put(spi->dev.of_node);
  538. }
  539. if (ACPI_COMPANION(&spi->dev))
  540. acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
  541. device_unregister(&spi->dev);
  542. }
  543. EXPORT_SYMBOL_GPL(spi_unregister_device);
  544. static void spi_match_master_to_boardinfo(struct spi_master *master,
  545. struct spi_board_info *bi)
  546. {
  547. struct spi_device *dev;
  548. if (master->bus_num != bi->bus_num)
  549. return;
  550. dev = spi_new_device(master, bi);
  551. if (!dev)
  552. dev_err(master->dev.parent, "can't create new device for %s\n",
  553. bi->modalias);
  554. }
  555. /**
  556. * spi_register_board_info - register SPI devices for a given board
  557. * @info: array of chip descriptors
  558. * @n: how many descriptors are provided
  559. * Context: can sleep
  560. *
  561. * Board-specific early init code calls this (probably during arch_initcall)
  562. * with segments of the SPI device table. Any device nodes are created later,
  563. * after the relevant parent SPI controller (bus_num) is defined. We keep
  564. * this table of devices forever, so that reloading a controller driver will
  565. * not make Linux forget about these hard-wired devices.
  566. *
  567. * Other code can also call this, e.g. a particular add-on board might provide
  568. * SPI devices through its expansion connector, so code initializing that board
  569. * would naturally declare its SPI devices.
  570. *
  571. * The board info passed can safely be __initdata ... but be careful of
  572. * any embedded pointers (platform_data, etc), they're copied as-is.
  573. *
  574. * Return: zero on success, else a negative error code.
  575. */
  576. int spi_register_board_info(struct spi_board_info const *info, unsigned n)
  577. {
  578. struct boardinfo *bi;
  579. int i;
  580. if (!n)
  581. return -EINVAL;
  582. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  583. if (!bi)
  584. return -ENOMEM;
  585. for (i = 0; i < n; i++, bi++, info++) {
  586. struct spi_master *master;
  587. memcpy(&bi->board_info, info, sizeof(*info));
  588. mutex_lock(&board_lock);
  589. list_add_tail(&bi->list, &board_list);
  590. list_for_each_entry(master, &spi_master_list, list)
  591. spi_match_master_to_boardinfo(master, &bi->board_info);
  592. mutex_unlock(&board_lock);
  593. }
  594. return 0;
  595. }
  596. /*-------------------------------------------------------------------------*/
  597. static void spi_set_cs(struct spi_device *spi, bool enable)
  598. {
  599. if (spi->mode & SPI_CS_HIGH)
  600. enable = !enable;
  601. if (gpio_is_valid(spi->cs_gpio))
  602. gpio_set_value(spi->cs_gpio, !enable);
  603. else if (spi->master->set_cs)
  604. spi->master->set_cs(spi, !enable);
  605. }
  606. #ifdef CONFIG_HAS_DMA
  607. static int spi_map_buf(struct spi_master *master, struct device *dev,
  608. struct sg_table *sgt, void *buf, size_t len,
  609. enum dma_data_direction dir)
  610. {
  611. const bool vmalloced_buf = is_vmalloc_addr(buf);
  612. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  613. #ifdef CONFIG_HIGHMEM
  614. const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
  615. (unsigned long)buf < (PKMAP_BASE +
  616. (LAST_PKMAP * PAGE_SIZE)));
  617. #else
  618. const bool kmap_buf = false;
  619. #endif
  620. int desc_len;
  621. int sgs;
  622. struct page *vm_page;
  623. void *sg_buf;
  624. size_t min;
  625. int i, ret;
  626. if (vmalloced_buf || kmap_buf) {
  627. desc_len = min_t(int, max_seg_size, PAGE_SIZE);
  628. sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
  629. } else if (virt_addr_valid(buf)) {
  630. desc_len = min_t(int, max_seg_size, master->max_dma_len);
  631. sgs = DIV_ROUND_UP(len, desc_len);
  632. } else {
  633. return -EINVAL;
  634. }
  635. ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
  636. if (ret != 0)
  637. return ret;
  638. for (i = 0; i < sgs; i++) {
  639. if (vmalloced_buf || kmap_buf) {
  640. /*
  641. * Next scatterlist entry size is the minimum between
  642. * the desc_len and the remaining buffer length that
  643. * fits in a page.
  644. */
  645. min = min_t(size_t, desc_len,
  646. min_t(size_t, len,
  647. PAGE_SIZE - offset_in_page(buf)));
  648. if (vmalloced_buf)
  649. vm_page = vmalloc_to_page(buf);
  650. else
  651. vm_page = kmap_to_page(buf);
  652. if (!vm_page) {
  653. sg_free_table(sgt);
  654. return -ENOMEM;
  655. }
  656. sg_set_page(&sgt->sgl[i], vm_page,
  657. min, offset_in_page(buf));
  658. } else {
  659. min = min_t(size_t, len, desc_len);
  660. sg_buf = buf;
  661. sg_set_buf(&sgt->sgl[i], sg_buf, min);
  662. }
  663. buf += min;
  664. len -= min;
  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.parent;
  695. if (master->dma_rx)
  696. rx_dev = master->dma_rx->device->dev;
  697. else
  698. rx_dev = master->dev.parent;
  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.parent;
  733. if (master->dma_rx)
  734. rx_dev = master->dma_rx->device->dev;
  735. else
  736. rx_dev = master->dev.parent;
  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 + 200; /* 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. udelay(xfer->delay_usecs);
  893. if (xfer->cs_change) {
  894. if (list_is_last(&xfer->transfer_list,
  895. &msg->transfers)) {
  896. keep_cs = true;
  897. } else {
  898. spi_set_cs(msg->spi, false);
  899. udelay(10);
  900. spi_set_cs(msg->spi, true);
  901. }
  902. }
  903. msg->actual_length += xfer->len;
  904. }
  905. out:
  906. if (ret != 0 || !keep_cs)
  907. spi_set_cs(msg->spi, false);
  908. if (msg->status == -EINPROGRESS)
  909. msg->status = ret;
  910. if (msg->status && master->handle_err)
  911. master->handle_err(master, msg);
  912. spi_res_release(master, msg);
  913. spi_finalize_current_message(master);
  914. return ret;
  915. }
  916. /**
  917. * spi_finalize_current_transfer - report completion of a transfer
  918. * @master: the master reporting completion
  919. *
  920. * Called by SPI drivers using the core transfer_one_message()
  921. * implementation to notify it that the current interrupt driven
  922. * transfer has finished and the next one may be scheduled.
  923. */
  924. void spi_finalize_current_transfer(struct spi_master *master)
  925. {
  926. complete(&master->xfer_completion);
  927. }
  928. EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
  929. /**
  930. * __spi_pump_messages - function which processes spi message queue
  931. * @master: master to process queue for
  932. * @in_kthread: true if we are in the context of the message pump thread
  933. *
  934. * This function checks if there is any spi message in the queue that
  935. * needs processing and if so call out to the driver to initialize hardware
  936. * and transfer each message.
  937. *
  938. * Note that it is called both from the kthread itself and also from
  939. * inside spi_sync(); the queue extraction handling at the top of the
  940. * function should deal with this safely.
  941. */
  942. static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
  943. {
  944. unsigned long flags;
  945. bool was_busy = false;
  946. int ret;
  947. /* Lock queue */
  948. spin_lock_irqsave(&master->queue_lock, flags);
  949. /* Make sure we are not already running a message */
  950. if (master->cur_msg) {
  951. spin_unlock_irqrestore(&master->queue_lock, flags);
  952. return;
  953. }
  954. /* If another context is idling the device then defer */
  955. if (master->idling) {
  956. kthread_queue_work(&master->kworker, &master->pump_messages);
  957. spin_unlock_irqrestore(&master->queue_lock, flags);
  958. return;
  959. }
  960. /* Check if the queue is idle */
  961. if (list_empty(&master->queue) || !master->running) {
  962. if (!master->busy) {
  963. spin_unlock_irqrestore(&master->queue_lock, flags);
  964. return;
  965. }
  966. /* Only do teardown in the thread */
  967. if (!in_kthread) {
  968. kthread_queue_work(&master->kworker,
  969. &master->pump_messages);
  970. spin_unlock_irqrestore(&master->queue_lock, flags);
  971. return;
  972. }
  973. master->busy = false;
  974. master->idling = true;
  975. spin_unlock_irqrestore(&master->queue_lock, flags);
  976. kfree(master->dummy_rx);
  977. master->dummy_rx = NULL;
  978. kfree(master->dummy_tx);
  979. master->dummy_tx = NULL;
  980. if (master->unprepare_transfer_hardware &&
  981. master->unprepare_transfer_hardware(master))
  982. dev_err(&master->dev,
  983. "failed to unprepare transfer hardware\n");
  984. if (master->auto_runtime_pm) {
  985. pm_runtime_mark_last_busy(master->dev.parent);
  986. pm_runtime_put_autosuspend(master->dev.parent);
  987. }
  988. trace_spi_master_idle(master);
  989. spin_lock_irqsave(&master->queue_lock, flags);
  990. master->idling = false;
  991. spin_unlock_irqrestore(&master->queue_lock, flags);
  992. return;
  993. }
  994. /* Extract head of queue */
  995. master->cur_msg =
  996. list_first_entry(&master->queue, struct spi_message, queue);
  997. list_del_init(&master->cur_msg->queue);
  998. if (master->busy)
  999. was_busy = true;
  1000. else
  1001. master->busy = true;
  1002. spin_unlock_irqrestore(&master->queue_lock, flags);
  1003. mutex_lock(&master->io_mutex);
  1004. if (!was_busy && master->auto_runtime_pm) {
  1005. ret = pm_runtime_get_sync(master->dev.parent);
  1006. if (ret < 0) {
  1007. dev_err(&master->dev, "Failed to power device: %d\n",
  1008. ret);
  1009. mutex_unlock(&master->io_mutex);
  1010. return;
  1011. }
  1012. }
  1013. if (!was_busy)
  1014. trace_spi_master_busy(master);
  1015. if (!was_busy && master->prepare_transfer_hardware) {
  1016. ret = master->prepare_transfer_hardware(master);
  1017. if (ret) {
  1018. dev_err(&master->dev,
  1019. "failed to prepare transfer hardware\n");
  1020. if (master->auto_runtime_pm)
  1021. pm_runtime_put(master->dev.parent);
  1022. mutex_unlock(&master->io_mutex);
  1023. return;
  1024. }
  1025. }
  1026. trace_spi_message_start(master->cur_msg);
  1027. if (master->prepare_message) {
  1028. ret = master->prepare_message(master, master->cur_msg);
  1029. if (ret) {
  1030. dev_err(&master->dev,
  1031. "failed to prepare message: %d\n", ret);
  1032. master->cur_msg->status = ret;
  1033. spi_finalize_current_message(master);
  1034. goto out;
  1035. }
  1036. master->cur_msg_prepared = true;
  1037. }
  1038. ret = spi_map_msg(master, master->cur_msg);
  1039. if (ret) {
  1040. master->cur_msg->status = ret;
  1041. spi_finalize_current_message(master);
  1042. goto out;
  1043. }
  1044. ret = master->transfer_one_message(master, master->cur_msg);
  1045. if (ret) {
  1046. dev_err(&master->dev,
  1047. "failed to transfer one message from queue\n");
  1048. goto out;
  1049. }
  1050. out:
  1051. mutex_unlock(&master->io_mutex);
  1052. /* Prod the scheduler in case transfer_one() was busy waiting */
  1053. if (!ret)
  1054. cond_resched();
  1055. }
  1056. /**
  1057. * spi_pump_messages - kthread work function which processes spi message queue
  1058. * @work: pointer to kthread work struct contained in the master struct
  1059. */
  1060. static void spi_pump_messages(struct kthread_work *work)
  1061. {
  1062. struct spi_master *master =
  1063. container_of(work, struct spi_master, pump_messages);
  1064. __spi_pump_messages(master, true);
  1065. }
  1066. static int spi_init_queue(struct spi_master *master)
  1067. {
  1068. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  1069. master->running = false;
  1070. master->busy = false;
  1071. kthread_init_worker(&master->kworker);
  1072. master->kworker_task = kthread_run(kthread_worker_fn,
  1073. &master->kworker, "%s",
  1074. dev_name(&master->dev));
  1075. if (IS_ERR(master->kworker_task)) {
  1076. dev_err(&master->dev, "failed to create message pump task\n");
  1077. return PTR_ERR(master->kworker_task);
  1078. }
  1079. kthread_init_work(&master->pump_messages, spi_pump_messages);
  1080. /*
  1081. * Master config will indicate if this controller should run the
  1082. * message pump with high (realtime) priority to reduce the transfer
  1083. * latency on the bus by minimising the delay between a transfer
  1084. * request and the scheduling of the message pump thread. Without this
  1085. * setting the message pump thread will remain at default priority.
  1086. */
  1087. if (master->rt) {
  1088. dev_info(&master->dev,
  1089. "will run message pump with realtime priority\n");
  1090. sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
  1091. }
  1092. return 0;
  1093. }
  1094. /**
  1095. * spi_get_next_queued_message() - called by driver to check for queued
  1096. * messages
  1097. * @master: the master to check for queued messages
  1098. *
  1099. * If there are more messages in the queue, the next message is returned from
  1100. * this call.
  1101. *
  1102. * Return: the next message in the queue, else NULL if the queue is empty.
  1103. */
  1104. struct spi_message *spi_get_next_queued_message(struct spi_master *master)
  1105. {
  1106. struct spi_message *next;
  1107. unsigned long flags;
  1108. /* get a pointer to the next message, if any */
  1109. spin_lock_irqsave(&master->queue_lock, flags);
  1110. next = list_first_entry_or_null(&master->queue, struct spi_message,
  1111. queue);
  1112. spin_unlock_irqrestore(&master->queue_lock, flags);
  1113. return next;
  1114. }
  1115. EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
  1116. /**
  1117. * spi_finalize_current_message() - the current message is complete
  1118. * @master: the master to return the message to
  1119. *
  1120. * Called by the driver to notify the core that the message in the front of the
  1121. * queue is complete and can be removed from the queue.
  1122. */
  1123. void spi_finalize_current_message(struct spi_master *master)
  1124. {
  1125. struct spi_message *mesg;
  1126. unsigned long flags;
  1127. int ret;
  1128. spin_lock_irqsave(&master->queue_lock, flags);
  1129. mesg = master->cur_msg;
  1130. spin_unlock_irqrestore(&master->queue_lock, flags);
  1131. spi_unmap_msg(master, mesg);
  1132. if (master->cur_msg_prepared && master->unprepare_message) {
  1133. ret = master->unprepare_message(master, mesg);
  1134. if (ret) {
  1135. dev_err(&master->dev,
  1136. "failed to unprepare message: %d\n", ret);
  1137. }
  1138. }
  1139. spin_lock_irqsave(&master->queue_lock, flags);
  1140. master->cur_msg = NULL;
  1141. master->cur_msg_prepared = false;
  1142. kthread_queue_work(&master->kworker, &master->pump_messages);
  1143. spin_unlock_irqrestore(&master->queue_lock, flags);
  1144. trace_spi_message_done(mesg);
  1145. mesg->state = NULL;
  1146. if (mesg->complete)
  1147. mesg->complete(mesg->context);
  1148. }
  1149. EXPORT_SYMBOL_GPL(spi_finalize_current_message);
  1150. static int spi_start_queue(struct spi_master *master)
  1151. {
  1152. unsigned long flags;
  1153. spin_lock_irqsave(&master->queue_lock, flags);
  1154. if (master->running || master->busy) {
  1155. spin_unlock_irqrestore(&master->queue_lock, flags);
  1156. return -EBUSY;
  1157. }
  1158. master->running = true;
  1159. master->cur_msg = NULL;
  1160. spin_unlock_irqrestore(&master->queue_lock, flags);
  1161. kthread_queue_work(&master->kworker, &master->pump_messages);
  1162. return 0;
  1163. }
  1164. static int spi_stop_queue(struct spi_master *master)
  1165. {
  1166. unsigned long flags;
  1167. unsigned limit = 500;
  1168. int ret = 0;
  1169. spin_lock_irqsave(&master->queue_lock, flags);
  1170. /*
  1171. * This is a bit lame, but is optimized for the common execution path.
  1172. * A wait_queue on the master->busy could be used, but then the common
  1173. * execution path (pump_messages) would be required to call wake_up or
  1174. * friends on every SPI message. Do this instead.
  1175. */
  1176. while ((!list_empty(&master->queue) || master->busy) && limit--) {
  1177. spin_unlock_irqrestore(&master->queue_lock, flags);
  1178. usleep_range(10000, 11000);
  1179. spin_lock_irqsave(&master->queue_lock, flags);
  1180. }
  1181. if (!list_empty(&master->queue) || master->busy)
  1182. ret = -EBUSY;
  1183. else
  1184. master->running = false;
  1185. spin_unlock_irqrestore(&master->queue_lock, flags);
  1186. if (ret) {
  1187. dev_warn(&master->dev,
  1188. "could not stop message queue\n");
  1189. return ret;
  1190. }
  1191. return ret;
  1192. }
  1193. static int spi_destroy_queue(struct spi_master *master)
  1194. {
  1195. int ret;
  1196. ret = spi_stop_queue(master);
  1197. /*
  1198. * kthread_flush_worker will block until all work is done.
  1199. * If the reason that stop_queue timed out is that the work will never
  1200. * finish, then it does no good to call flush/stop thread, so
  1201. * return anyway.
  1202. */
  1203. if (ret) {
  1204. dev_err(&master->dev, "problem destroying queue\n");
  1205. return ret;
  1206. }
  1207. kthread_flush_worker(&master->kworker);
  1208. kthread_stop(master->kworker_task);
  1209. return 0;
  1210. }
  1211. static int __spi_queued_transfer(struct spi_device *spi,
  1212. struct spi_message *msg,
  1213. bool need_pump)
  1214. {
  1215. struct spi_master *master = spi->master;
  1216. unsigned long flags;
  1217. spin_lock_irqsave(&master->queue_lock, flags);
  1218. if (!master->running) {
  1219. spin_unlock_irqrestore(&master->queue_lock, flags);
  1220. return -ESHUTDOWN;
  1221. }
  1222. msg->actual_length = 0;
  1223. msg->status = -EINPROGRESS;
  1224. list_add_tail(&msg->queue, &master->queue);
  1225. if (!master->busy && need_pump)
  1226. kthread_queue_work(&master->kworker, &master->pump_messages);
  1227. spin_unlock_irqrestore(&master->queue_lock, flags);
  1228. return 0;
  1229. }
  1230. /**
  1231. * spi_queued_transfer - transfer function for queued transfers
  1232. * @spi: spi device which is requesting transfer
  1233. * @msg: spi message which is to handled is queued to driver queue
  1234. *
  1235. * Return: zero on success, else a negative error code.
  1236. */
  1237. static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
  1238. {
  1239. return __spi_queued_transfer(spi, msg, true);
  1240. }
  1241. static int spi_master_initialize_queue(struct spi_master *master)
  1242. {
  1243. int ret;
  1244. master->transfer = spi_queued_transfer;
  1245. if (!master->transfer_one_message)
  1246. master->transfer_one_message = spi_transfer_one_message;
  1247. /* Initialize and start queue */
  1248. ret = spi_init_queue(master);
  1249. if (ret) {
  1250. dev_err(&master->dev, "problem initializing queue\n");
  1251. goto err_init_queue;
  1252. }
  1253. master->queued = true;
  1254. ret = spi_start_queue(master);
  1255. if (ret) {
  1256. dev_err(&master->dev, "problem starting queue\n");
  1257. goto err_start_queue;
  1258. }
  1259. return 0;
  1260. err_start_queue:
  1261. spi_destroy_queue(master);
  1262. err_init_queue:
  1263. return ret;
  1264. }
  1265. /*-------------------------------------------------------------------------*/
  1266. #if defined(CONFIG_OF)
  1267. static struct spi_device *
  1268. of_register_spi_device(struct spi_master *master, struct device_node *nc)
  1269. {
  1270. struct spi_device *spi;
  1271. int rc;
  1272. u32 value;
  1273. /* Alloc an spi_device */
  1274. spi = spi_alloc_device(master);
  1275. if (!spi) {
  1276. dev_err(&master->dev, "spi_device alloc error for %s\n",
  1277. nc->full_name);
  1278. rc = -ENOMEM;
  1279. goto err_out;
  1280. }
  1281. /* Select device driver */
  1282. rc = of_modalias_node(nc, spi->modalias,
  1283. sizeof(spi->modalias));
  1284. if (rc < 0) {
  1285. dev_err(&master->dev, "cannot find modalias for %s\n",
  1286. nc->full_name);
  1287. goto err_out;
  1288. }
  1289. /* Device address */
  1290. rc = of_property_read_u32(nc, "reg", &value);
  1291. if (rc) {
  1292. dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
  1293. nc->full_name, rc);
  1294. goto err_out;
  1295. }
  1296. spi->chip_select = value;
  1297. /* Mode (clock phase/polarity/etc.) */
  1298. if (of_find_property(nc, "spi-cpha", NULL))
  1299. spi->mode |= SPI_CPHA;
  1300. if (of_find_property(nc, "spi-cpol", NULL))
  1301. spi->mode |= SPI_CPOL;
  1302. if (of_find_property(nc, "spi-cs-high", NULL))
  1303. spi->mode |= SPI_CS_HIGH;
  1304. if (of_find_property(nc, "spi-3wire", NULL))
  1305. spi->mode |= SPI_3WIRE;
  1306. if (of_find_property(nc, "spi-lsb-first", NULL))
  1307. spi->mode |= SPI_LSB_FIRST;
  1308. /* Device DUAL/QUAD mode */
  1309. if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
  1310. switch (value) {
  1311. case 1:
  1312. break;
  1313. case 2:
  1314. spi->mode |= SPI_TX_DUAL;
  1315. break;
  1316. case 4:
  1317. spi->mode |= SPI_TX_QUAD;
  1318. break;
  1319. default:
  1320. dev_warn(&master->dev,
  1321. "spi-tx-bus-width %d not supported\n",
  1322. value);
  1323. break;
  1324. }
  1325. }
  1326. if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
  1327. switch (value) {
  1328. case 1:
  1329. break;
  1330. case 2:
  1331. spi->mode |= SPI_RX_DUAL;
  1332. break;
  1333. case 4:
  1334. spi->mode |= SPI_RX_QUAD;
  1335. break;
  1336. default:
  1337. dev_warn(&master->dev,
  1338. "spi-rx-bus-width %d not supported\n",
  1339. value);
  1340. break;
  1341. }
  1342. }
  1343. /* Device speed */
  1344. rc = of_property_read_u32(nc, "spi-max-frequency", &value);
  1345. if (rc) {
  1346. dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
  1347. nc->full_name, rc);
  1348. goto err_out;
  1349. }
  1350. spi->max_speed_hz = value;
  1351. /* Store a pointer to the node in the device structure */
  1352. of_node_get(nc);
  1353. spi->dev.of_node = nc;
  1354. /* Register the new device */
  1355. rc = spi_add_device(spi);
  1356. if (rc) {
  1357. dev_err(&master->dev, "spi_device register error %s\n",
  1358. nc->full_name);
  1359. goto err_of_node_put;
  1360. }
  1361. return spi;
  1362. err_of_node_put:
  1363. of_node_put(nc);
  1364. err_out:
  1365. spi_dev_put(spi);
  1366. return ERR_PTR(rc);
  1367. }
  1368. /**
  1369. * of_register_spi_devices() - Register child devices onto the SPI bus
  1370. * @master: Pointer to spi_master device
  1371. *
  1372. * Registers an spi_device for each child node of master node which has a 'reg'
  1373. * property.
  1374. */
  1375. static void of_register_spi_devices(struct spi_master *master)
  1376. {
  1377. struct spi_device *spi;
  1378. struct device_node *nc;
  1379. if (!master->dev.of_node)
  1380. return;
  1381. for_each_available_child_of_node(master->dev.of_node, nc) {
  1382. if (of_node_test_and_set_flag(nc, OF_POPULATED))
  1383. continue;
  1384. spi = of_register_spi_device(master, nc);
  1385. if (IS_ERR(spi)) {
  1386. dev_warn(&master->dev, "Failed to create SPI device for %s\n",
  1387. nc->full_name);
  1388. of_node_clear_flag(nc, OF_POPULATED);
  1389. }
  1390. }
  1391. }
  1392. #else
  1393. static void of_register_spi_devices(struct spi_master *master) { }
  1394. #endif
  1395. #ifdef CONFIG_ACPI
  1396. static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
  1397. {
  1398. struct spi_device *spi = data;
  1399. struct spi_master *master = spi->master;
  1400. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  1401. struct acpi_resource_spi_serialbus *sb;
  1402. sb = &ares->data.spi_serial_bus;
  1403. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
  1404. /*
  1405. * ACPI DeviceSelection numbering is handled by the
  1406. * host controller driver in Windows and can vary
  1407. * from driver to driver. In Linux we always expect
  1408. * 0 .. max - 1 so we need to ask the driver to
  1409. * translate between the two schemes.
  1410. */
  1411. if (master->fw_translate_cs) {
  1412. int cs = master->fw_translate_cs(master,
  1413. sb->device_selection);
  1414. if (cs < 0)
  1415. return cs;
  1416. spi->chip_select = cs;
  1417. } else {
  1418. spi->chip_select = sb->device_selection;
  1419. }
  1420. spi->max_speed_hz = sb->connection_speed;
  1421. if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
  1422. spi->mode |= SPI_CPHA;
  1423. if (sb->clock_polarity == ACPI_SPI_START_HIGH)
  1424. spi->mode |= SPI_CPOL;
  1425. if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
  1426. spi->mode |= SPI_CS_HIGH;
  1427. }
  1428. } else if (spi->irq < 0) {
  1429. struct resource r;
  1430. if (acpi_dev_resource_interrupt(ares, 0, &r))
  1431. spi->irq = r.start;
  1432. }
  1433. /* Always tell the ACPI core to skip this resource */
  1434. return 1;
  1435. }
  1436. static acpi_status acpi_register_spi_device(struct spi_master *master,
  1437. struct acpi_device *adev)
  1438. {
  1439. struct list_head resource_list;
  1440. struct spi_device *spi;
  1441. int ret;
  1442. if (acpi_bus_get_status(adev) || !adev->status.present ||
  1443. acpi_device_enumerated(adev))
  1444. return AE_OK;
  1445. spi = spi_alloc_device(master);
  1446. if (!spi) {
  1447. dev_err(&master->dev, "failed to allocate SPI device for %s\n",
  1448. dev_name(&adev->dev));
  1449. return AE_NO_MEMORY;
  1450. }
  1451. ACPI_COMPANION_SET(&spi->dev, adev);
  1452. spi->irq = -1;
  1453. INIT_LIST_HEAD(&resource_list);
  1454. ret = acpi_dev_get_resources(adev, &resource_list,
  1455. acpi_spi_add_resource, spi);
  1456. acpi_dev_free_resource_list(&resource_list);
  1457. if (ret < 0 || !spi->max_speed_hz) {
  1458. spi_dev_put(spi);
  1459. return AE_OK;
  1460. }
  1461. if (spi->irq < 0)
  1462. spi->irq = acpi_dev_gpio_irq_get(adev, 0);
  1463. acpi_device_set_enumerated(adev);
  1464. adev->power.flags.ignore_parent = true;
  1465. strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
  1466. if (spi_add_device(spi)) {
  1467. adev->power.flags.ignore_parent = false;
  1468. dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
  1469. dev_name(&adev->dev));
  1470. spi_dev_put(spi);
  1471. }
  1472. return AE_OK;
  1473. }
  1474. static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
  1475. void *data, void **return_value)
  1476. {
  1477. struct spi_master *master = data;
  1478. struct acpi_device *adev;
  1479. if (acpi_bus_get_device(handle, &adev))
  1480. return AE_OK;
  1481. return acpi_register_spi_device(master, adev);
  1482. }
  1483. static void acpi_register_spi_devices(struct spi_master *master)
  1484. {
  1485. acpi_status status;
  1486. acpi_handle handle;
  1487. handle = ACPI_HANDLE(master->dev.parent);
  1488. if (!handle)
  1489. return;
  1490. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  1491. acpi_spi_add_device, NULL,
  1492. master, NULL);
  1493. if (ACPI_FAILURE(status))
  1494. dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
  1495. }
  1496. #else
  1497. static inline void acpi_register_spi_devices(struct spi_master *master) {}
  1498. #endif /* CONFIG_ACPI */
  1499. static void spi_master_release(struct device *dev)
  1500. {
  1501. struct spi_master *master;
  1502. master = container_of(dev, struct spi_master, dev);
  1503. kfree(master);
  1504. }
  1505. static struct class spi_master_class = {
  1506. .name = "spi_master",
  1507. .owner = THIS_MODULE,
  1508. .dev_release = spi_master_release,
  1509. .dev_groups = spi_master_groups,
  1510. };
  1511. /**
  1512. * spi_alloc_master - allocate SPI master controller
  1513. * @dev: the controller, possibly using the platform_bus
  1514. * @size: how much zeroed driver-private data to allocate; the pointer to this
  1515. * memory is in the driver_data field of the returned device,
  1516. * accessible with spi_master_get_devdata().
  1517. * Context: can sleep
  1518. *
  1519. * This call is used only by SPI master controller drivers, which are the
  1520. * only ones directly touching chip registers. It's how they allocate
  1521. * an spi_master structure, prior to calling spi_register_master().
  1522. *
  1523. * This must be called from context that can sleep.
  1524. *
  1525. * The caller is responsible for assigning the bus number and initializing
  1526. * the master's methods before calling spi_register_master(); and (after errors
  1527. * adding the device) calling spi_master_put() to prevent a memory leak.
  1528. *
  1529. * Return: the SPI master structure on success, else NULL.
  1530. */
  1531. struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
  1532. {
  1533. struct spi_master *master;
  1534. if (!dev)
  1535. return NULL;
  1536. master = kzalloc(size + sizeof(*master), GFP_KERNEL);
  1537. if (!master)
  1538. return NULL;
  1539. device_initialize(&master->dev);
  1540. master->bus_num = -1;
  1541. master->num_chipselect = 1;
  1542. master->dev.class = &spi_master_class;
  1543. master->dev.parent = dev;
  1544. pm_suspend_ignore_children(&master->dev, true);
  1545. spi_master_set_devdata(master, &master[1]);
  1546. return master;
  1547. }
  1548. EXPORT_SYMBOL_GPL(spi_alloc_master);
  1549. #ifdef CONFIG_OF
  1550. static int of_spi_register_master(struct spi_master *master)
  1551. {
  1552. int nb, i, *cs;
  1553. struct device_node *np = master->dev.of_node;
  1554. if (!np)
  1555. return 0;
  1556. nb = of_gpio_named_count(np, "cs-gpios");
  1557. master->num_chipselect = max_t(int, nb, master->num_chipselect);
  1558. /* Return error only for an incorrectly formed cs-gpios property */
  1559. if (nb == 0 || nb == -ENOENT)
  1560. return 0;
  1561. else if (nb < 0)
  1562. return nb;
  1563. cs = devm_kzalloc(&master->dev,
  1564. sizeof(int) * master->num_chipselect,
  1565. GFP_KERNEL);
  1566. master->cs_gpios = cs;
  1567. if (!master->cs_gpios)
  1568. return -ENOMEM;
  1569. for (i = 0; i < master->num_chipselect; i++)
  1570. cs[i] = -ENOENT;
  1571. for (i = 0; i < nb; i++)
  1572. cs[i] = of_get_named_gpio(np, "cs-gpios", i);
  1573. return 0;
  1574. }
  1575. #else
  1576. static int of_spi_register_master(struct spi_master *master)
  1577. {
  1578. return 0;
  1579. }
  1580. #endif
  1581. /**
  1582. * spi_register_master - register SPI master controller
  1583. * @master: initialized master, originally from spi_alloc_master()
  1584. * Context: can sleep
  1585. *
  1586. * SPI master controllers connect to their drivers using some non-SPI bus,
  1587. * such as the platform bus. The final stage of probe() in that code
  1588. * includes calling spi_register_master() to hook up to this SPI bus glue.
  1589. *
  1590. * SPI controllers use board specific (often SOC specific) bus numbers,
  1591. * and board-specific addressing for SPI devices combines those numbers
  1592. * with chip select numbers. Since SPI does not directly support dynamic
  1593. * device identification, boards need configuration tables telling which
  1594. * chip is at which address.
  1595. *
  1596. * This must be called from context that can sleep. It returns zero on
  1597. * success, else a negative error code (dropping the master's refcount).
  1598. * After a successful return, the caller is responsible for calling
  1599. * spi_unregister_master().
  1600. *
  1601. * Return: zero on success, else a negative error code.
  1602. */
  1603. int spi_register_master(struct spi_master *master)
  1604. {
  1605. static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
  1606. struct device *dev = master->dev.parent;
  1607. struct boardinfo *bi;
  1608. int status = -ENODEV;
  1609. int dynamic = 0;
  1610. if (!dev)
  1611. return -ENODEV;
  1612. status = of_spi_register_master(master);
  1613. if (status)
  1614. return status;
  1615. /* even if it's just one always-selected device, there must
  1616. * be at least one chipselect
  1617. */
  1618. if (master->num_chipselect == 0)
  1619. return -EINVAL;
  1620. if ((master->bus_num < 0) && master->dev.of_node)
  1621. master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
  1622. /* convention: dynamically assigned bus IDs count down from the max */
  1623. if (master->bus_num < 0) {
  1624. /* FIXME switch to an IDR based scheme, something like
  1625. * I2C now uses, so we can't run out of "dynamic" IDs
  1626. */
  1627. master->bus_num = atomic_dec_return(&dyn_bus_id);
  1628. dynamic = 1;
  1629. }
  1630. INIT_LIST_HEAD(&master->queue);
  1631. spin_lock_init(&master->queue_lock);
  1632. spin_lock_init(&master->bus_lock_spinlock);
  1633. mutex_init(&master->bus_lock_mutex);
  1634. mutex_init(&master->io_mutex);
  1635. master->bus_lock_flag = 0;
  1636. init_completion(&master->xfer_completion);
  1637. if (!master->max_dma_len)
  1638. master->max_dma_len = INT_MAX;
  1639. /* register the device, then userspace will see it.
  1640. * registration fails if the bus ID is in use.
  1641. */
  1642. dev_set_name(&master->dev, "spi%u", master->bus_num);
  1643. status = device_add(&master->dev);
  1644. if (status < 0)
  1645. goto done;
  1646. dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
  1647. dynamic ? " (dynamic)" : "");
  1648. /* If we're using a queued driver, start the queue */
  1649. if (master->transfer)
  1650. dev_info(dev, "master is unqueued, this is deprecated\n");
  1651. else {
  1652. status = spi_master_initialize_queue(master);
  1653. if (status) {
  1654. device_del(&master->dev);
  1655. goto done;
  1656. }
  1657. }
  1658. /* add statistics */
  1659. spin_lock_init(&master->statistics.lock);
  1660. mutex_lock(&board_lock);
  1661. list_add_tail(&master->list, &spi_master_list);
  1662. list_for_each_entry(bi, &board_list, list)
  1663. spi_match_master_to_boardinfo(master, &bi->board_info);
  1664. mutex_unlock(&board_lock);
  1665. /* Register devices from the device tree and ACPI */
  1666. of_register_spi_devices(master);
  1667. acpi_register_spi_devices(master);
  1668. done:
  1669. return status;
  1670. }
  1671. EXPORT_SYMBOL_GPL(spi_register_master);
  1672. static void devm_spi_unregister(struct device *dev, void *res)
  1673. {
  1674. spi_unregister_master(*(struct spi_master **)res);
  1675. }
  1676. /**
  1677. * dev_spi_register_master - register managed SPI master controller
  1678. * @dev: device managing SPI master
  1679. * @master: initialized master, originally from spi_alloc_master()
  1680. * Context: can sleep
  1681. *
  1682. * Register a SPI device as with spi_register_master() which will
  1683. * automatically be unregister
  1684. *
  1685. * Return: zero on success, else a negative error code.
  1686. */
  1687. int devm_spi_register_master(struct device *dev, struct spi_master *master)
  1688. {
  1689. struct spi_master **ptr;
  1690. int ret;
  1691. ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
  1692. if (!ptr)
  1693. return -ENOMEM;
  1694. ret = spi_register_master(master);
  1695. if (!ret) {
  1696. *ptr = master;
  1697. devres_add(dev, ptr);
  1698. } else {
  1699. devres_free(ptr);
  1700. }
  1701. return ret;
  1702. }
  1703. EXPORT_SYMBOL_GPL(devm_spi_register_master);
  1704. static int __unregister(struct device *dev, void *null)
  1705. {
  1706. spi_unregister_device(to_spi_device(dev));
  1707. return 0;
  1708. }
  1709. /**
  1710. * spi_unregister_master - unregister SPI master controller
  1711. * @master: the master being unregistered
  1712. * Context: can sleep
  1713. *
  1714. * This call is used only by SPI master controller drivers, which are the
  1715. * only ones directly touching chip registers.
  1716. *
  1717. * This must be called from context that can sleep.
  1718. */
  1719. void spi_unregister_master(struct spi_master *master)
  1720. {
  1721. int dummy;
  1722. if (master->queued) {
  1723. if (spi_destroy_queue(master))
  1724. dev_err(&master->dev, "queue remove failed\n");
  1725. }
  1726. mutex_lock(&board_lock);
  1727. list_del(&master->list);
  1728. mutex_unlock(&board_lock);
  1729. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  1730. device_unregister(&master->dev);
  1731. }
  1732. EXPORT_SYMBOL_GPL(spi_unregister_master);
  1733. int spi_master_suspend(struct spi_master *master)
  1734. {
  1735. int ret;
  1736. /* Basically no-ops for non-queued masters */
  1737. if (!master->queued)
  1738. return 0;
  1739. ret = spi_stop_queue(master);
  1740. if (ret)
  1741. dev_err(&master->dev, "queue stop failed\n");
  1742. return ret;
  1743. }
  1744. EXPORT_SYMBOL_GPL(spi_master_suspend);
  1745. int spi_master_resume(struct spi_master *master)
  1746. {
  1747. int ret;
  1748. if (!master->queued)
  1749. return 0;
  1750. ret = spi_start_queue(master);
  1751. if (ret)
  1752. dev_err(&master->dev, "queue restart failed\n");
  1753. return ret;
  1754. }
  1755. EXPORT_SYMBOL_GPL(spi_master_resume);
  1756. static int __spi_master_match(struct device *dev, const void *data)
  1757. {
  1758. struct spi_master *m;
  1759. const u16 *bus_num = data;
  1760. m = container_of(dev, struct spi_master, dev);
  1761. return m->bus_num == *bus_num;
  1762. }
  1763. /**
  1764. * spi_busnum_to_master - look up master associated with bus_num
  1765. * @bus_num: the master's bus number
  1766. * Context: can sleep
  1767. *
  1768. * This call may be used with devices that are registered after
  1769. * arch init time. It returns a refcounted pointer to the relevant
  1770. * spi_master (which the caller must release), or NULL if there is
  1771. * no such master registered.
  1772. *
  1773. * Return: the SPI master structure on success, else NULL.
  1774. */
  1775. struct spi_master *spi_busnum_to_master(u16 bus_num)
  1776. {
  1777. struct device *dev;
  1778. struct spi_master *master = NULL;
  1779. dev = class_find_device(&spi_master_class, NULL, &bus_num,
  1780. __spi_master_match);
  1781. if (dev)
  1782. master = container_of(dev, struct spi_master, dev);
  1783. /* reference got in class_find_device */
  1784. return master;
  1785. }
  1786. EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  1787. /*-------------------------------------------------------------------------*/
  1788. /* Core methods for SPI resource management */
  1789. /**
  1790. * spi_res_alloc - allocate a spi resource that is life-cycle managed
  1791. * during the processing of a spi_message while using
  1792. * spi_transfer_one
  1793. * @spi: the spi device for which we allocate memory
  1794. * @release: the release code to execute for this resource
  1795. * @size: size to alloc and return
  1796. * @gfp: GFP allocation flags
  1797. *
  1798. * Return: the pointer to the allocated data
  1799. *
  1800. * This may get enhanced in the future to allocate from a memory pool
  1801. * of the @spi_device or @spi_master to avoid repeated allocations.
  1802. */
  1803. void *spi_res_alloc(struct spi_device *spi,
  1804. spi_res_release_t release,
  1805. size_t size, gfp_t gfp)
  1806. {
  1807. struct spi_res *sres;
  1808. sres = kzalloc(sizeof(*sres) + size, gfp);
  1809. if (!sres)
  1810. return NULL;
  1811. INIT_LIST_HEAD(&sres->entry);
  1812. sres->release = release;
  1813. return sres->data;
  1814. }
  1815. EXPORT_SYMBOL_GPL(spi_res_alloc);
  1816. /**
  1817. * spi_res_free - free an spi resource
  1818. * @res: pointer to the custom data of a resource
  1819. *
  1820. */
  1821. void spi_res_free(void *res)
  1822. {
  1823. struct spi_res *sres = container_of(res, struct spi_res, data);
  1824. if (!res)
  1825. return;
  1826. WARN_ON(!list_empty(&sres->entry));
  1827. kfree(sres);
  1828. }
  1829. EXPORT_SYMBOL_GPL(spi_res_free);
  1830. /**
  1831. * spi_res_add - add a spi_res to the spi_message
  1832. * @message: the spi message
  1833. * @res: the spi_resource
  1834. */
  1835. void spi_res_add(struct spi_message *message, void *res)
  1836. {
  1837. struct spi_res *sres = container_of(res, struct spi_res, data);
  1838. WARN_ON(!list_empty(&sres->entry));
  1839. list_add_tail(&sres->entry, &message->resources);
  1840. }
  1841. EXPORT_SYMBOL_GPL(spi_res_add);
  1842. /**
  1843. * spi_res_release - release all spi resources for this message
  1844. * @master: the @spi_master
  1845. * @message: the @spi_message
  1846. */
  1847. void spi_res_release(struct spi_master *master,
  1848. struct spi_message *message)
  1849. {
  1850. struct spi_res *res;
  1851. while (!list_empty(&message->resources)) {
  1852. res = list_last_entry(&message->resources,
  1853. struct spi_res, entry);
  1854. if (res->release)
  1855. res->release(master, message, res->data);
  1856. list_del(&res->entry);
  1857. kfree(res);
  1858. }
  1859. }
  1860. EXPORT_SYMBOL_GPL(spi_res_release);
  1861. /*-------------------------------------------------------------------------*/
  1862. /* Core methods for spi_message alterations */
  1863. static void __spi_replace_transfers_release(struct spi_master *master,
  1864. struct spi_message *msg,
  1865. void *res)
  1866. {
  1867. struct spi_replaced_transfers *rxfer = res;
  1868. size_t i;
  1869. /* call extra callback if requested */
  1870. if (rxfer->release)
  1871. rxfer->release(master, msg, res);
  1872. /* insert replaced transfers back into the message */
  1873. list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
  1874. /* remove the formerly inserted entries */
  1875. for (i = 0; i < rxfer->inserted; i++)
  1876. list_del(&rxfer->inserted_transfers[i].transfer_list);
  1877. }
  1878. /**
  1879. * spi_replace_transfers - replace transfers with several transfers
  1880. * and register change with spi_message.resources
  1881. * @msg: the spi_message we work upon
  1882. * @xfer_first: the first spi_transfer we want to replace
  1883. * @remove: number of transfers to remove
  1884. * @insert: the number of transfers we want to insert instead
  1885. * @release: extra release code necessary in some circumstances
  1886. * @extradatasize: extra data to allocate (with alignment guarantees
  1887. * of struct @spi_transfer)
  1888. * @gfp: gfp flags
  1889. *
  1890. * Returns: pointer to @spi_replaced_transfers,
  1891. * PTR_ERR(...) in case of errors.
  1892. */
  1893. struct spi_replaced_transfers *spi_replace_transfers(
  1894. struct spi_message *msg,
  1895. struct spi_transfer *xfer_first,
  1896. size_t remove,
  1897. size_t insert,
  1898. spi_replaced_release_t release,
  1899. size_t extradatasize,
  1900. gfp_t gfp)
  1901. {
  1902. struct spi_replaced_transfers *rxfer;
  1903. struct spi_transfer *xfer;
  1904. size_t i;
  1905. /* allocate the structure using spi_res */
  1906. rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
  1907. insert * sizeof(struct spi_transfer)
  1908. + sizeof(struct spi_replaced_transfers)
  1909. + extradatasize,
  1910. gfp);
  1911. if (!rxfer)
  1912. return ERR_PTR(-ENOMEM);
  1913. /* the release code to invoke before running the generic release */
  1914. rxfer->release = release;
  1915. /* assign extradata */
  1916. if (extradatasize)
  1917. rxfer->extradata =
  1918. &rxfer->inserted_transfers[insert];
  1919. /* init the replaced_transfers list */
  1920. INIT_LIST_HEAD(&rxfer->replaced_transfers);
  1921. /* assign the list_entry after which we should reinsert
  1922. * the @replaced_transfers - it may be spi_message.messages!
  1923. */
  1924. rxfer->replaced_after = xfer_first->transfer_list.prev;
  1925. /* remove the requested number of transfers */
  1926. for (i = 0; i < remove; i++) {
  1927. /* if the entry after replaced_after it is msg->transfers
  1928. * then we have been requested to remove more transfers
  1929. * than are in the list
  1930. */
  1931. if (rxfer->replaced_after->next == &msg->transfers) {
  1932. dev_err(&msg->spi->dev,
  1933. "requested to remove more spi_transfers than are available\n");
  1934. /* insert replaced transfers back into the message */
  1935. list_splice(&rxfer->replaced_transfers,
  1936. rxfer->replaced_after);
  1937. /* free the spi_replace_transfer structure */
  1938. spi_res_free(rxfer);
  1939. /* and return with an error */
  1940. return ERR_PTR(-EINVAL);
  1941. }
  1942. /* remove the entry after replaced_after from list of
  1943. * transfers and add it to list of replaced_transfers
  1944. */
  1945. list_move_tail(rxfer->replaced_after->next,
  1946. &rxfer->replaced_transfers);
  1947. }
  1948. /* create copy of the given xfer with identical settings
  1949. * based on the first transfer to get removed
  1950. */
  1951. for (i = 0; i < insert; i++) {
  1952. /* we need to run in reverse order */
  1953. xfer = &rxfer->inserted_transfers[insert - 1 - i];
  1954. /* copy all spi_transfer data */
  1955. memcpy(xfer, xfer_first, sizeof(*xfer));
  1956. /* add to list */
  1957. list_add(&xfer->transfer_list, rxfer->replaced_after);
  1958. /* clear cs_change and delay_usecs for all but the last */
  1959. if (i) {
  1960. xfer->cs_change = false;
  1961. xfer->delay_usecs = 0;
  1962. }
  1963. }
  1964. /* set up inserted */
  1965. rxfer->inserted = insert;
  1966. /* and register it with spi_res/spi_message */
  1967. spi_res_add(msg, rxfer);
  1968. return rxfer;
  1969. }
  1970. EXPORT_SYMBOL_GPL(spi_replace_transfers);
  1971. static int __spi_split_transfer_maxsize(struct spi_master *master,
  1972. struct spi_message *msg,
  1973. struct spi_transfer **xferp,
  1974. size_t maxsize,
  1975. gfp_t gfp)
  1976. {
  1977. struct spi_transfer *xfer = *xferp, *xfers;
  1978. struct spi_replaced_transfers *srt;
  1979. size_t offset;
  1980. size_t count, i;
  1981. /* warn once about this fact that we are splitting a transfer */
  1982. dev_warn_once(&msg->spi->dev,
  1983. "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
  1984. xfer->len, maxsize);
  1985. /* calculate how many we have to replace */
  1986. count = DIV_ROUND_UP(xfer->len, maxsize);
  1987. /* create replacement */
  1988. srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
  1989. if (IS_ERR(srt))
  1990. return PTR_ERR(srt);
  1991. xfers = srt->inserted_transfers;
  1992. /* now handle each of those newly inserted spi_transfers
  1993. * note that the replacements spi_transfers all are preset
  1994. * to the same values as *xferp, so tx_buf, rx_buf and len
  1995. * are all identical (as well as most others)
  1996. * so we just have to fix up len and the pointers.
  1997. *
  1998. * this also includes support for the depreciated
  1999. * spi_message.is_dma_mapped interface
  2000. */
  2001. /* the first transfer just needs the length modified, so we
  2002. * run it outside the loop
  2003. */
  2004. xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
  2005. /* all the others need rx_buf/tx_buf also set */
  2006. for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
  2007. /* update rx_buf, tx_buf and dma */
  2008. if (xfers[i].rx_buf)
  2009. xfers[i].rx_buf += offset;
  2010. if (xfers[i].rx_dma)
  2011. xfers[i].rx_dma += offset;
  2012. if (xfers[i].tx_buf)
  2013. xfers[i].tx_buf += offset;
  2014. if (xfers[i].tx_dma)
  2015. xfers[i].tx_dma += offset;
  2016. /* update length */
  2017. xfers[i].len = min(maxsize, xfers[i].len - offset);
  2018. }
  2019. /* we set up xferp to the last entry we have inserted,
  2020. * so that we skip those already split transfers
  2021. */
  2022. *xferp = &xfers[count - 1];
  2023. /* increment statistics counters */
  2024. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
  2025. transfers_split_maxsize);
  2026. SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
  2027. transfers_split_maxsize);
  2028. return 0;
  2029. }
  2030. /**
  2031. * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
  2032. * when an individual transfer exceeds a
  2033. * certain size
  2034. * @master: the @spi_master for this transfer
  2035. * @msg: the @spi_message to transform
  2036. * @maxsize: the maximum when to apply this
  2037. * @gfp: GFP allocation flags
  2038. *
  2039. * Return: status of transformation
  2040. */
  2041. int spi_split_transfers_maxsize(struct spi_master *master,
  2042. struct spi_message *msg,
  2043. size_t maxsize,
  2044. gfp_t gfp)
  2045. {
  2046. struct spi_transfer *xfer;
  2047. int ret;
  2048. /* iterate over the transfer_list,
  2049. * but note that xfer is advanced to the last transfer inserted
  2050. * to avoid checking sizes again unnecessarily (also xfer does
  2051. * potentiall belong to a different list by the time the
  2052. * replacement has happened
  2053. */
  2054. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  2055. if (xfer->len > maxsize) {
  2056. ret = __spi_split_transfer_maxsize(
  2057. master, msg, &xfer, maxsize, gfp);
  2058. if (ret)
  2059. return ret;
  2060. }
  2061. }
  2062. return 0;
  2063. }
  2064. EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
  2065. /*-------------------------------------------------------------------------*/
  2066. /* Core methods for SPI master protocol drivers. Some of the
  2067. * other core methods are currently defined as inline functions.
  2068. */
  2069. static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
  2070. {
  2071. if (master->bits_per_word_mask) {
  2072. /* Only 32 bits fit in the mask */
  2073. if (bits_per_word > 32)
  2074. return -EINVAL;
  2075. if (!(master->bits_per_word_mask &
  2076. SPI_BPW_MASK(bits_per_word)))
  2077. return -EINVAL;
  2078. }
  2079. return 0;
  2080. }
  2081. /**
  2082. * spi_setup - setup SPI mode and clock rate
  2083. * @spi: the device whose settings are being modified
  2084. * Context: can sleep, and no requests are queued to the device
  2085. *
  2086. * SPI protocol drivers may need to update the transfer mode if the
  2087. * device doesn't work with its default. They may likewise need
  2088. * to update clock rates or word sizes from initial values. This function
  2089. * changes those settings, and must be called from a context that can sleep.
  2090. * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
  2091. * effect the next time the device is selected and data is transferred to
  2092. * or from it. When this function returns, the spi device is deselected.
  2093. *
  2094. * Note that this call will fail if the protocol driver specifies an option
  2095. * that the underlying controller or its driver does not support. For
  2096. * example, not all hardware supports wire transfers using nine bit words,
  2097. * LSB-first wire encoding, or active-high chipselects.
  2098. *
  2099. * Return: zero on success, else a negative error code.
  2100. */
  2101. int spi_setup(struct spi_device *spi)
  2102. {
  2103. unsigned bad_bits, ugly_bits;
  2104. int status;
  2105. /* check mode to prevent that DUAL and QUAD set at the same time
  2106. */
  2107. if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
  2108. ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
  2109. dev_err(&spi->dev,
  2110. "setup: can not select dual and quad at the same time\n");
  2111. return -EINVAL;
  2112. }
  2113. /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
  2114. */
  2115. if ((spi->mode & SPI_3WIRE) && (spi->mode &
  2116. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
  2117. return -EINVAL;
  2118. /* help drivers fail *cleanly* when they need options
  2119. * that aren't supported with their current master
  2120. */
  2121. bad_bits = spi->mode & ~spi->master->mode_bits;
  2122. ugly_bits = bad_bits &
  2123. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  2124. if (ugly_bits) {
  2125. dev_warn(&spi->dev,
  2126. "setup: ignoring unsupported mode bits %x\n",
  2127. ugly_bits);
  2128. spi->mode &= ~ugly_bits;
  2129. bad_bits &= ~ugly_bits;
  2130. }
  2131. if (bad_bits) {
  2132. dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
  2133. bad_bits);
  2134. return -EINVAL;
  2135. }
  2136. if (!spi->bits_per_word)
  2137. spi->bits_per_word = 8;
  2138. status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
  2139. if (status)
  2140. return status;
  2141. if (!spi->max_speed_hz)
  2142. spi->max_speed_hz = spi->master->max_speed_hz;
  2143. if (spi->master->setup)
  2144. status = spi->master->setup(spi);
  2145. spi_set_cs(spi, false);
  2146. dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
  2147. (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  2148. (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
  2149. (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
  2150. (spi->mode & SPI_3WIRE) ? "3wire, " : "",
  2151. (spi->mode & SPI_LOOP) ? "loopback, " : "",
  2152. spi->bits_per_word, spi->max_speed_hz,
  2153. status);
  2154. return status;
  2155. }
  2156. EXPORT_SYMBOL_GPL(spi_setup);
  2157. static int __spi_validate(struct spi_device *spi, struct spi_message *message)
  2158. {
  2159. struct spi_master *master = spi->master;
  2160. struct spi_transfer *xfer;
  2161. int w_size;
  2162. if (list_empty(&message->transfers))
  2163. return -EINVAL;
  2164. /* Half-duplex links include original MicroWire, and ones with
  2165. * only one data pin like SPI_3WIRE (switches direction) or where
  2166. * either MOSI or MISO is missing. They can also be caused by
  2167. * software limitations.
  2168. */
  2169. if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  2170. || (spi->mode & SPI_3WIRE)) {
  2171. unsigned flags = master->flags;
  2172. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  2173. if (xfer->rx_buf && xfer->tx_buf)
  2174. return -EINVAL;
  2175. if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  2176. return -EINVAL;
  2177. if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  2178. return -EINVAL;
  2179. }
  2180. }
  2181. /**
  2182. * Set transfer bits_per_word and max speed as spi device default if
  2183. * it is not set for this transfer.
  2184. * Set transfer tx_nbits and rx_nbits as single transfer default
  2185. * (SPI_NBITS_SINGLE) if it is not set for this transfer.
  2186. */
  2187. message->frame_length = 0;
  2188. list_for_each_entry(xfer, &message->transfers, transfer_list) {
  2189. message->frame_length += xfer->len;
  2190. if (!xfer->bits_per_word)
  2191. xfer->bits_per_word = spi->bits_per_word;
  2192. if (!xfer->speed_hz)
  2193. xfer->speed_hz = spi->max_speed_hz;
  2194. if (!xfer->speed_hz)
  2195. xfer->speed_hz = master->max_speed_hz;
  2196. if (master->max_speed_hz &&
  2197. xfer->speed_hz > master->max_speed_hz)
  2198. xfer->speed_hz = master->max_speed_hz;
  2199. if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
  2200. return -EINVAL;
  2201. /*
  2202. * SPI transfer length should be multiple of SPI word size
  2203. * where SPI word size should be power-of-two multiple
  2204. */
  2205. if (xfer->bits_per_word <= 8)
  2206. w_size = 1;
  2207. else if (xfer->bits_per_word <= 16)
  2208. w_size = 2;
  2209. else
  2210. w_size = 4;
  2211. /* No partial transfers accepted */
  2212. if (xfer->len % w_size)
  2213. return -EINVAL;
  2214. if (xfer->speed_hz && master->min_speed_hz &&
  2215. xfer->speed_hz < master->min_speed_hz)
  2216. return -EINVAL;
  2217. if (xfer->tx_buf && !xfer->tx_nbits)
  2218. xfer->tx_nbits = SPI_NBITS_SINGLE;
  2219. if (xfer->rx_buf && !xfer->rx_nbits)
  2220. xfer->rx_nbits = SPI_NBITS_SINGLE;
  2221. /* check transfer tx/rx_nbits:
  2222. * 1. check the value matches one of single, dual and quad
  2223. * 2. check tx/rx_nbits match the mode in spi_device
  2224. */
  2225. if (xfer->tx_buf) {
  2226. if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
  2227. xfer->tx_nbits != SPI_NBITS_DUAL &&
  2228. xfer->tx_nbits != SPI_NBITS_QUAD)
  2229. return -EINVAL;
  2230. if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
  2231. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  2232. return -EINVAL;
  2233. if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
  2234. !(spi->mode & SPI_TX_QUAD))
  2235. return -EINVAL;
  2236. }
  2237. /* check transfer rx_nbits */
  2238. if (xfer->rx_buf) {
  2239. if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
  2240. xfer->rx_nbits != SPI_NBITS_DUAL &&
  2241. xfer->rx_nbits != SPI_NBITS_QUAD)
  2242. return -EINVAL;
  2243. if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
  2244. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  2245. return -EINVAL;
  2246. if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
  2247. !(spi->mode & SPI_RX_QUAD))
  2248. return -EINVAL;
  2249. }
  2250. }
  2251. message->status = -EINPROGRESS;
  2252. return 0;
  2253. }
  2254. static int __spi_async(struct spi_device *spi, struct spi_message *message)
  2255. {
  2256. struct spi_master *master = spi->master;
  2257. message->spi = spi;
  2258. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
  2259. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
  2260. trace_spi_message_submit(message);
  2261. return master->transfer(spi, message);
  2262. }
  2263. /**
  2264. * spi_async - asynchronous SPI transfer
  2265. * @spi: device with which data will be exchanged
  2266. * @message: describes the data transfers, including completion callback
  2267. * Context: any (irqs may be blocked, etc)
  2268. *
  2269. * This call may be used in_irq and other contexts which can't sleep,
  2270. * as well as from task contexts which can sleep.
  2271. *
  2272. * The completion callback is invoked in a context which can't sleep.
  2273. * Before that invocation, the value of message->status is undefined.
  2274. * When the callback is issued, message->status holds either zero (to
  2275. * indicate complete success) or a negative error code. After that
  2276. * callback returns, the driver which issued the transfer request may
  2277. * deallocate the associated memory; it's no longer in use by any SPI
  2278. * core or controller driver code.
  2279. *
  2280. * Note that although all messages to a spi_device are handled in
  2281. * FIFO order, messages may go to different devices in other orders.
  2282. * Some device might be higher priority, or have various "hard" access
  2283. * time requirements, for example.
  2284. *
  2285. * On detection of any fault during the transfer, processing of
  2286. * the entire message is aborted, and the device is deselected.
  2287. * Until returning from the associated message completion callback,
  2288. * no other spi_message queued to that device will be processed.
  2289. * (This rule applies equally to all the synchronous transfer calls,
  2290. * which are wrappers around this core asynchronous primitive.)
  2291. *
  2292. * Return: zero on success, else a negative error code.
  2293. */
  2294. int spi_async(struct spi_device *spi, struct spi_message *message)
  2295. {
  2296. struct spi_master *master = spi->master;
  2297. int ret;
  2298. unsigned long flags;
  2299. ret = __spi_validate(spi, message);
  2300. if (ret != 0)
  2301. return ret;
  2302. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2303. if (master->bus_lock_flag)
  2304. ret = -EBUSY;
  2305. else
  2306. ret = __spi_async(spi, message);
  2307. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2308. return ret;
  2309. }
  2310. EXPORT_SYMBOL_GPL(spi_async);
  2311. /**
  2312. * spi_async_locked - version of spi_async with exclusive bus usage
  2313. * @spi: device with which data will be exchanged
  2314. * @message: describes the data transfers, including completion callback
  2315. * Context: any (irqs may be blocked, etc)
  2316. *
  2317. * This call may be used in_irq and other contexts which can't sleep,
  2318. * as well as from task contexts which can sleep.
  2319. *
  2320. * The completion callback is invoked in a context which can't sleep.
  2321. * Before that invocation, the value of message->status is undefined.
  2322. * When the callback is issued, message->status holds either zero (to
  2323. * indicate complete success) or a negative error code. After that
  2324. * callback returns, the driver which issued the transfer request may
  2325. * deallocate the associated memory; it's no longer in use by any SPI
  2326. * core or controller driver code.
  2327. *
  2328. * Note that although all messages to a spi_device are handled in
  2329. * FIFO order, messages may go to different devices in other orders.
  2330. * Some device might be higher priority, or have various "hard" access
  2331. * time requirements, for example.
  2332. *
  2333. * On detection of any fault during the transfer, processing of
  2334. * the entire message is aborted, and the device is deselected.
  2335. * Until returning from the associated message completion callback,
  2336. * no other spi_message queued to that device will be processed.
  2337. * (This rule applies equally to all the synchronous transfer calls,
  2338. * which are wrappers around this core asynchronous primitive.)
  2339. *
  2340. * Return: zero on success, else a negative error code.
  2341. */
  2342. int spi_async_locked(struct spi_device *spi, struct spi_message *message)
  2343. {
  2344. struct spi_master *master = spi->master;
  2345. int ret;
  2346. unsigned long flags;
  2347. ret = __spi_validate(spi, message);
  2348. if (ret != 0)
  2349. return ret;
  2350. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2351. ret = __spi_async(spi, message);
  2352. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2353. return ret;
  2354. }
  2355. EXPORT_SYMBOL_GPL(spi_async_locked);
  2356. int spi_flash_read(struct spi_device *spi,
  2357. struct spi_flash_read_message *msg)
  2358. {
  2359. struct spi_master *master = spi->master;
  2360. struct device *rx_dev = NULL;
  2361. int ret;
  2362. if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
  2363. msg->addr_nbits == SPI_NBITS_DUAL) &&
  2364. !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
  2365. return -EINVAL;
  2366. if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
  2367. msg->addr_nbits == SPI_NBITS_QUAD) &&
  2368. !(spi->mode & SPI_TX_QUAD))
  2369. return -EINVAL;
  2370. if (msg->data_nbits == SPI_NBITS_DUAL &&
  2371. !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
  2372. return -EINVAL;
  2373. if (msg->data_nbits == SPI_NBITS_QUAD &&
  2374. !(spi->mode & SPI_RX_QUAD))
  2375. return -EINVAL;
  2376. if (master->auto_runtime_pm) {
  2377. ret = pm_runtime_get_sync(master->dev.parent);
  2378. if (ret < 0) {
  2379. dev_err(&master->dev, "Failed to power device: %d\n",
  2380. ret);
  2381. return ret;
  2382. }
  2383. }
  2384. mutex_lock(&master->bus_lock_mutex);
  2385. mutex_lock(&master->io_mutex);
  2386. if (master->dma_rx) {
  2387. rx_dev = master->dma_rx->device->dev;
  2388. ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
  2389. msg->buf, msg->len,
  2390. DMA_FROM_DEVICE);
  2391. if (!ret)
  2392. msg->cur_msg_mapped = true;
  2393. }
  2394. ret = master->spi_flash_read(spi, msg);
  2395. if (msg->cur_msg_mapped)
  2396. spi_unmap_buf(master, rx_dev, &msg->rx_sg,
  2397. DMA_FROM_DEVICE);
  2398. mutex_unlock(&master->io_mutex);
  2399. mutex_unlock(&master->bus_lock_mutex);
  2400. if (master->auto_runtime_pm)
  2401. pm_runtime_put(master->dev.parent);
  2402. return ret;
  2403. }
  2404. EXPORT_SYMBOL_GPL(spi_flash_read);
  2405. /*-------------------------------------------------------------------------*/
  2406. /* Utility methods for SPI master protocol drivers, layered on
  2407. * top of the core. Some other utility methods are defined as
  2408. * inline functions.
  2409. */
  2410. static void spi_complete(void *arg)
  2411. {
  2412. complete(arg);
  2413. }
  2414. static int __spi_sync(struct spi_device *spi, struct spi_message *message)
  2415. {
  2416. DECLARE_COMPLETION_ONSTACK(done);
  2417. int status;
  2418. struct spi_master *master = spi->master;
  2419. unsigned long flags;
  2420. status = __spi_validate(spi, message);
  2421. if (status != 0)
  2422. return status;
  2423. message->complete = spi_complete;
  2424. message->context = &done;
  2425. message->spi = spi;
  2426. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
  2427. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
  2428. /* If we're not using the legacy transfer method then we will
  2429. * try to transfer in the calling context so special case.
  2430. * This code would be less tricky if we could remove the
  2431. * support for driver implemented message queues.
  2432. */
  2433. if (master->transfer == spi_queued_transfer) {
  2434. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2435. trace_spi_message_submit(message);
  2436. status = __spi_queued_transfer(spi, message, false);
  2437. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2438. } else {
  2439. status = spi_async_locked(spi, message);
  2440. }
  2441. if (status == 0) {
  2442. /* Push out the messages in the calling context if we
  2443. * can.
  2444. */
  2445. if (master->transfer == spi_queued_transfer) {
  2446. SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
  2447. spi_sync_immediate);
  2448. SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
  2449. spi_sync_immediate);
  2450. __spi_pump_messages(master, false);
  2451. }
  2452. wait_for_completion(&done);
  2453. status = message->status;
  2454. }
  2455. message->context = NULL;
  2456. return status;
  2457. }
  2458. /**
  2459. * spi_sync - blocking/synchronous SPI data transfers
  2460. * @spi: device with which data will be exchanged
  2461. * @message: describes the data transfers
  2462. * Context: can sleep
  2463. *
  2464. * This call may only be used from a context that may sleep. The sleep
  2465. * is non-interruptible, and has no timeout. Low-overhead controller
  2466. * drivers may DMA directly into and out of the message buffers.
  2467. *
  2468. * Note that the SPI device's chip select is active during the message,
  2469. * and then is normally disabled between messages. Drivers for some
  2470. * frequently-used devices may want to minimize costs of selecting a chip,
  2471. * by leaving it selected in anticipation that the next message will go
  2472. * to the same chip. (That may increase power usage.)
  2473. *
  2474. * Also, the caller is guaranteeing that the memory associated with the
  2475. * message will not be freed before this call returns.
  2476. *
  2477. * Return: zero on success, else a negative error code.
  2478. */
  2479. int spi_sync(struct spi_device *spi, struct spi_message *message)
  2480. {
  2481. int ret;
  2482. mutex_lock(&spi->master->bus_lock_mutex);
  2483. ret = __spi_sync(spi, message);
  2484. mutex_unlock(&spi->master->bus_lock_mutex);
  2485. return ret;
  2486. }
  2487. EXPORT_SYMBOL_GPL(spi_sync);
  2488. /**
  2489. * spi_sync_locked - version of spi_sync with exclusive bus usage
  2490. * @spi: device with which data will be exchanged
  2491. * @message: describes the data transfers
  2492. * Context: can sleep
  2493. *
  2494. * This call may only be used from a context that may sleep. The sleep
  2495. * is non-interruptible, and has no timeout. Low-overhead controller
  2496. * drivers may DMA directly into and out of the message buffers.
  2497. *
  2498. * This call should be used by drivers that require exclusive access to the
  2499. * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
  2500. * be released by a spi_bus_unlock call when the exclusive access is over.
  2501. *
  2502. * Return: zero on success, else a negative error code.
  2503. */
  2504. int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
  2505. {
  2506. return __spi_sync(spi, message);
  2507. }
  2508. EXPORT_SYMBOL_GPL(spi_sync_locked);
  2509. /**
  2510. * spi_bus_lock - obtain a lock for exclusive SPI bus usage
  2511. * @master: SPI bus master that should be locked for exclusive bus access
  2512. * Context: can sleep
  2513. *
  2514. * This call may only be used from a context that may sleep. The sleep
  2515. * is non-interruptible, and has no timeout.
  2516. *
  2517. * This call should be used by drivers that require exclusive access to the
  2518. * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
  2519. * exclusive access is over. Data transfer must be done by spi_sync_locked
  2520. * and spi_async_locked calls when the SPI bus lock is held.
  2521. *
  2522. * Return: always zero.
  2523. */
  2524. int spi_bus_lock(struct spi_master *master)
  2525. {
  2526. unsigned long flags;
  2527. mutex_lock(&master->bus_lock_mutex);
  2528. spin_lock_irqsave(&master->bus_lock_spinlock, flags);
  2529. master->bus_lock_flag = 1;
  2530. spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
  2531. /* mutex remains locked until spi_bus_unlock is called */
  2532. return 0;
  2533. }
  2534. EXPORT_SYMBOL_GPL(spi_bus_lock);
  2535. /**
  2536. * spi_bus_unlock - release the lock for exclusive SPI bus usage
  2537. * @master: SPI bus master that was locked for exclusive bus access
  2538. * Context: can sleep
  2539. *
  2540. * This call may only be used from a context that may sleep. The sleep
  2541. * is non-interruptible, and has no timeout.
  2542. *
  2543. * This call releases an SPI bus lock previously obtained by an spi_bus_lock
  2544. * call.
  2545. *
  2546. * Return: always zero.
  2547. */
  2548. int spi_bus_unlock(struct spi_master *master)
  2549. {
  2550. master->bus_lock_flag = 0;
  2551. mutex_unlock(&master->bus_lock_mutex);
  2552. return 0;
  2553. }
  2554. EXPORT_SYMBOL_GPL(spi_bus_unlock);
  2555. /* portable code must never pass more than 32 bytes */
  2556. #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
  2557. static u8 *buf;
  2558. /**
  2559. * spi_write_then_read - SPI synchronous write followed by read
  2560. * @spi: device with which data will be exchanged
  2561. * @txbuf: data to be written (need not be dma-safe)
  2562. * @n_tx: size of txbuf, in bytes
  2563. * @rxbuf: buffer into which data will be read (need not be dma-safe)
  2564. * @n_rx: size of rxbuf, in bytes
  2565. * Context: can sleep
  2566. *
  2567. * This performs a half duplex MicroWire style transaction with the
  2568. * device, sending txbuf and then reading rxbuf. The return value
  2569. * is zero for success, else a negative errno status code.
  2570. * This call may only be used from a context that may sleep.
  2571. *
  2572. * Parameters to this routine are always copied using a small buffer;
  2573. * portable code should never use this for more than 32 bytes.
  2574. * Performance-sensitive or bulk transfer code should instead use
  2575. * spi_{async,sync}() calls with dma-safe buffers.
  2576. *
  2577. * Return: zero on success, else a negative error code.
  2578. */
  2579. int spi_write_then_read(struct spi_device *spi,
  2580. const void *txbuf, unsigned n_tx,
  2581. void *rxbuf, unsigned n_rx)
  2582. {
  2583. static DEFINE_MUTEX(lock);
  2584. int status;
  2585. struct spi_message message;
  2586. struct spi_transfer x[2];
  2587. u8 *local_buf;
  2588. /* Use preallocated DMA-safe buffer if we can. We can't avoid
  2589. * copying here, (as a pure convenience thing), but we can
  2590. * keep heap costs out of the hot path unless someone else is
  2591. * using the pre-allocated buffer or the transfer is too large.
  2592. */
  2593. if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
  2594. local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
  2595. GFP_KERNEL | GFP_DMA);
  2596. if (!local_buf)
  2597. return -ENOMEM;
  2598. } else {
  2599. local_buf = buf;
  2600. }
  2601. spi_message_init(&message);
  2602. memset(x, 0, sizeof(x));
  2603. if (n_tx) {
  2604. x[0].len = n_tx;
  2605. spi_message_add_tail(&x[0], &message);
  2606. }
  2607. if (n_rx) {
  2608. x[1].len = n_rx;
  2609. spi_message_add_tail(&x[1], &message);
  2610. }
  2611. memcpy(local_buf, txbuf, n_tx);
  2612. x[0].tx_buf = local_buf;
  2613. x[1].rx_buf = local_buf + n_tx;
  2614. /* do the i/o */
  2615. status = spi_sync(spi, &message);
  2616. if (status == 0)
  2617. memcpy(rxbuf, x[1].rx_buf, n_rx);
  2618. if (x[0].tx_buf == buf)
  2619. mutex_unlock(&lock);
  2620. else
  2621. kfree(local_buf);
  2622. return status;
  2623. }
  2624. EXPORT_SYMBOL_GPL(spi_write_then_read);
  2625. /*-------------------------------------------------------------------------*/
  2626. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  2627. static int __spi_of_device_match(struct device *dev, void *data)
  2628. {
  2629. return dev->of_node == data;
  2630. }
  2631. /* must call put_device() when done with returned spi_device device */
  2632. static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
  2633. {
  2634. struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
  2635. __spi_of_device_match);
  2636. return dev ? to_spi_device(dev) : NULL;
  2637. }
  2638. static int __spi_of_master_match(struct device *dev, const void *data)
  2639. {
  2640. return dev->of_node == data;
  2641. }
  2642. /* the spi masters are not using spi_bus, so we find it with another way */
  2643. static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
  2644. {
  2645. struct device *dev;
  2646. dev = class_find_device(&spi_master_class, NULL, node,
  2647. __spi_of_master_match);
  2648. if (!dev)
  2649. return NULL;
  2650. /* reference got in class_find_device */
  2651. return container_of(dev, struct spi_master, dev);
  2652. }
  2653. static int of_spi_notify(struct notifier_block *nb, unsigned long action,
  2654. void *arg)
  2655. {
  2656. struct of_reconfig_data *rd = arg;
  2657. struct spi_master *master;
  2658. struct spi_device *spi;
  2659. switch (of_reconfig_get_state_change(action, arg)) {
  2660. case OF_RECONFIG_CHANGE_ADD:
  2661. master = of_find_spi_master_by_node(rd->dn->parent);
  2662. if (master == NULL)
  2663. return NOTIFY_OK; /* not for us */
  2664. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  2665. put_device(&master->dev);
  2666. return NOTIFY_OK;
  2667. }
  2668. spi = of_register_spi_device(master, rd->dn);
  2669. put_device(&master->dev);
  2670. if (IS_ERR(spi)) {
  2671. pr_err("%s: failed to create for '%s'\n",
  2672. __func__, rd->dn->full_name);
  2673. of_node_clear_flag(rd->dn, OF_POPULATED);
  2674. return notifier_from_errno(PTR_ERR(spi));
  2675. }
  2676. break;
  2677. case OF_RECONFIG_CHANGE_REMOVE:
  2678. /* already depopulated? */
  2679. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  2680. return NOTIFY_OK;
  2681. /* find our device by node */
  2682. spi = of_find_spi_device_by_node(rd->dn);
  2683. if (spi == NULL)
  2684. return NOTIFY_OK; /* no? not meant for us */
  2685. /* unregister takes one ref away */
  2686. spi_unregister_device(spi);
  2687. /* and put the reference of the find */
  2688. put_device(&spi->dev);
  2689. break;
  2690. }
  2691. return NOTIFY_OK;
  2692. }
  2693. static struct notifier_block spi_of_notifier = {
  2694. .notifier_call = of_spi_notify,
  2695. };
  2696. #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2697. extern struct notifier_block spi_of_notifier;
  2698. #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
  2699. #if IS_ENABLED(CONFIG_ACPI)
  2700. static int spi_acpi_master_match(struct device *dev, const void *data)
  2701. {
  2702. return ACPI_COMPANION(dev->parent) == data;
  2703. }
  2704. static int spi_acpi_device_match(struct device *dev, void *data)
  2705. {
  2706. return ACPI_COMPANION(dev) == data;
  2707. }
  2708. static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev)
  2709. {
  2710. struct device *dev;
  2711. dev = class_find_device(&spi_master_class, NULL, adev,
  2712. spi_acpi_master_match);
  2713. if (!dev)
  2714. return NULL;
  2715. return container_of(dev, struct spi_master, dev);
  2716. }
  2717. static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
  2718. {
  2719. struct device *dev;
  2720. dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
  2721. return dev ? to_spi_device(dev) : NULL;
  2722. }
  2723. static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
  2724. void *arg)
  2725. {
  2726. struct acpi_device *adev = arg;
  2727. struct spi_master *master;
  2728. struct spi_device *spi;
  2729. switch (value) {
  2730. case ACPI_RECONFIG_DEVICE_ADD:
  2731. master = acpi_spi_find_master_by_adev(adev->parent);
  2732. if (!master)
  2733. break;
  2734. acpi_register_spi_device(master, adev);
  2735. put_device(&master->dev);
  2736. break;
  2737. case ACPI_RECONFIG_DEVICE_REMOVE:
  2738. if (!acpi_device_enumerated(adev))
  2739. break;
  2740. spi = acpi_spi_find_device_by_adev(adev);
  2741. if (!spi)
  2742. break;
  2743. spi_unregister_device(spi);
  2744. put_device(&spi->dev);
  2745. break;
  2746. }
  2747. return NOTIFY_OK;
  2748. }
  2749. static struct notifier_block spi_acpi_notifier = {
  2750. .notifier_call = acpi_spi_notify,
  2751. };
  2752. #else
  2753. extern struct notifier_block spi_acpi_notifier;
  2754. #endif
  2755. static int __init spi_init(void)
  2756. {
  2757. int status;
  2758. buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
  2759. if (!buf) {
  2760. status = -ENOMEM;
  2761. goto err0;
  2762. }
  2763. status = bus_register(&spi_bus_type);
  2764. if (status < 0)
  2765. goto err1;
  2766. status = class_register(&spi_master_class);
  2767. if (status < 0)
  2768. goto err2;
  2769. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  2770. WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
  2771. if (IS_ENABLED(CONFIG_ACPI))
  2772. WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
  2773. return 0;
  2774. err2:
  2775. bus_unregister(&spi_bus_type);
  2776. err1:
  2777. kfree(buf);
  2778. buf = NULL;
  2779. err0:
  2780. return status;
  2781. }
  2782. /* board_info is normally registered in arch_initcall(),
  2783. * but even essential drivers wait till later
  2784. *
  2785. * REVISIT only boardinfo really needs static linking. the rest (device and
  2786. * driver registration) _could_ be dynamically linked (modular) ... costs
  2787. * include needing to have boardinfo data structures be much more public.
  2788. */
  2789. postcore_initcall(spi_init);