spi.c 89 KB

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