spi.c 87 KB

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