regmap.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register map access API
  4. //
  5. // Copyright 2011 Wolfson Microelectronics plc
  6. //
  7. // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. #include <linux/device.h>
  9. #include <linux/slab.h>
  10. #include <linux/export.h>
  11. #include <linux/mutex.h>
  12. #include <linux/err.h>
  13. #include <linux/property.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/sched.h>
  16. #include <linux/delay.h>
  17. #include <linux/log2.h>
  18. #include <linux/hwspinlock.h>
  19. #include <linux/unaligned.h>
  20. #define CREATE_TRACE_POINTS
  21. #include "trace.h"
  22. #include "internal.h"
  23. /*
  24. * Sometimes for failures during very early init the trace
  25. * infrastructure isn't available early enough to be used. For this
  26. * sort of problem defining LOG_DEVICE will add printks for basic
  27. * register I/O on a specific device.
  28. */
  29. #undef LOG_DEVICE
  30. #ifdef LOG_DEVICE
  31. static inline bool regmap_should_log(struct regmap *map)
  32. {
  33. return (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0);
  34. }
  35. #else
  36. static inline bool regmap_should_log(struct regmap *map) { return false; }
  37. #endif
  38. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  39. unsigned int mask, unsigned int val,
  40. bool *change, bool force_write);
  41. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  42. unsigned int *val);
  43. static int _regmap_bus_read(void *context, unsigned int reg,
  44. unsigned int *val);
  45. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  46. unsigned int val);
  47. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  48. unsigned int val);
  49. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  50. unsigned int val);
  51. bool regmap_reg_in_ranges(unsigned int reg,
  52. const struct regmap_range *ranges,
  53. unsigned int nranges)
  54. {
  55. const struct regmap_range *r;
  56. int i;
  57. for (i = 0, r = ranges; i < nranges; i++, r++)
  58. if (regmap_reg_in_range(reg, r))
  59. return true;
  60. return false;
  61. }
  62. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  63. bool regmap_check_range_table(struct regmap *map, unsigned int reg,
  64. const struct regmap_access_table *table)
  65. {
  66. /* Check "no ranges" first */
  67. if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
  68. return false;
  69. /* In case zero "yes ranges" are supplied, any reg is OK */
  70. if (!table->n_yes_ranges)
  71. return true;
  72. return regmap_reg_in_ranges(reg, table->yes_ranges,
  73. table->n_yes_ranges);
  74. }
  75. EXPORT_SYMBOL_GPL(regmap_check_range_table);
  76. bool regmap_writeable(struct regmap *map, unsigned int reg)
  77. {
  78. if (map->max_register_is_set && reg > map->max_register)
  79. return false;
  80. if (map->writeable_reg)
  81. return map->writeable_reg(map->dev, reg);
  82. if (map->wr_table)
  83. return regmap_check_range_table(map, reg, map->wr_table);
  84. return true;
  85. }
  86. bool regmap_cached(struct regmap *map, unsigned int reg)
  87. {
  88. int ret;
  89. unsigned int val;
  90. if (map->cache_type == REGCACHE_NONE)
  91. return false;
  92. if (!map->cache_ops)
  93. return false;
  94. if (map->max_register_is_set && reg > map->max_register)
  95. return false;
  96. map->lock(map->lock_arg);
  97. ret = regcache_read(map, reg, &val);
  98. map->unlock(map->lock_arg);
  99. if (ret)
  100. return false;
  101. return true;
  102. }
  103. bool regmap_readable(struct regmap *map, unsigned int reg)
  104. {
  105. if (!map->reg_read)
  106. return false;
  107. if (map->max_register_is_set && reg > map->max_register)
  108. return false;
  109. if (map->format.format_write)
  110. return false;
  111. if (map->readable_reg)
  112. return map->readable_reg(map->dev, reg);
  113. if (map->rd_table)
  114. return regmap_check_range_table(map, reg, map->rd_table);
  115. return true;
  116. }
  117. bool regmap_volatile(struct regmap *map, unsigned int reg)
  118. {
  119. if (!map->format.format_write && !regmap_readable(map, reg))
  120. return false;
  121. if (map->volatile_reg)
  122. return map->volatile_reg(map->dev, reg);
  123. if (map->volatile_table)
  124. return regmap_check_range_table(map, reg, map->volatile_table);
  125. if (map->cache_ops)
  126. return false;
  127. else
  128. return true;
  129. }
  130. bool regmap_precious(struct regmap *map, unsigned int reg)
  131. {
  132. if (!regmap_readable(map, reg))
  133. return false;
  134. if (map->precious_reg)
  135. return map->precious_reg(map->dev, reg);
  136. if (map->precious_table)
  137. return regmap_check_range_table(map, reg, map->precious_table);
  138. return false;
  139. }
  140. bool regmap_writeable_noinc(struct regmap *map, unsigned int reg)
  141. {
  142. if (map->writeable_noinc_reg)
  143. return map->writeable_noinc_reg(map->dev, reg);
  144. if (map->wr_noinc_table)
  145. return regmap_check_range_table(map, reg, map->wr_noinc_table);
  146. return true;
  147. }
  148. bool regmap_readable_noinc(struct regmap *map, unsigned int reg)
  149. {
  150. if (map->readable_noinc_reg)
  151. return map->readable_noinc_reg(map->dev, reg);
  152. if (map->rd_noinc_table)
  153. return regmap_check_range_table(map, reg, map->rd_noinc_table);
  154. return true;
  155. }
  156. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  157. size_t num)
  158. {
  159. unsigned int i;
  160. for (i = 0; i < num; i++)
  161. if (!regmap_volatile(map, reg + regmap_get_offset(map, i)))
  162. return false;
  163. return true;
  164. }
  165. static void regmap_format_12_20_write(struct regmap *map,
  166. unsigned int reg, unsigned int val)
  167. {
  168. u8 *out = map->work_buf;
  169. out[0] = reg >> 4;
  170. out[1] = (reg << 4) | (val >> 16);
  171. out[2] = val >> 8;
  172. out[3] = val;
  173. }
  174. static void regmap_format_2_6_write(struct regmap *map,
  175. unsigned int reg, unsigned int val)
  176. {
  177. u8 *out = map->work_buf;
  178. *out = (reg << 6) | val;
  179. }
  180. static void regmap_format_4_12_write(struct regmap *map,
  181. unsigned int reg, unsigned int val)
  182. {
  183. __be16 *out = map->work_buf;
  184. *out = cpu_to_be16((reg << 12) | val);
  185. }
  186. static void regmap_format_7_9_write(struct regmap *map,
  187. unsigned int reg, unsigned int val)
  188. {
  189. __be16 *out = map->work_buf;
  190. *out = cpu_to_be16((reg << 9) | val);
  191. }
  192. static void regmap_format_7_17_write(struct regmap *map,
  193. unsigned int reg, unsigned int val)
  194. {
  195. u8 *out = map->work_buf;
  196. out[2] = val;
  197. out[1] = val >> 8;
  198. out[0] = (val >> 16) | (reg << 1);
  199. }
  200. static void regmap_format_10_14_write(struct regmap *map,
  201. unsigned int reg, unsigned int val)
  202. {
  203. u8 *out = map->work_buf;
  204. out[2] = val;
  205. out[1] = (val >> 8) | (reg << 6);
  206. out[0] = reg >> 2;
  207. }
  208. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  209. {
  210. u8 *b = buf;
  211. b[0] = val << shift;
  212. }
  213. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  214. {
  215. put_unaligned_be16(val << shift, buf);
  216. }
  217. static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
  218. {
  219. put_unaligned_le16(val << shift, buf);
  220. }
  221. static void regmap_format_16_native(void *buf, unsigned int val,
  222. unsigned int shift)
  223. {
  224. u16 v = val << shift;
  225. memcpy(buf, &v, sizeof(v));
  226. }
  227. static void regmap_format_24_be(void *buf, unsigned int val, unsigned int shift)
  228. {
  229. put_unaligned_be24(val << shift, buf);
  230. }
  231. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  232. {
  233. put_unaligned_be32(val << shift, buf);
  234. }
  235. static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
  236. {
  237. put_unaligned_le32(val << shift, buf);
  238. }
  239. static void regmap_format_32_native(void *buf, unsigned int val,
  240. unsigned int shift)
  241. {
  242. u32 v = val << shift;
  243. memcpy(buf, &v, sizeof(v));
  244. }
  245. static void regmap_parse_inplace_noop(void *buf)
  246. {
  247. }
  248. static unsigned int regmap_parse_8(const void *buf)
  249. {
  250. const u8 *b = buf;
  251. return b[0];
  252. }
  253. static unsigned int regmap_parse_16_be(const void *buf)
  254. {
  255. return get_unaligned_be16(buf);
  256. }
  257. static unsigned int regmap_parse_16_le(const void *buf)
  258. {
  259. return get_unaligned_le16(buf);
  260. }
  261. static void regmap_parse_16_be_inplace(void *buf)
  262. {
  263. u16 v = get_unaligned_be16(buf);
  264. memcpy(buf, &v, sizeof(v));
  265. }
  266. static void regmap_parse_16_le_inplace(void *buf)
  267. {
  268. u16 v = get_unaligned_le16(buf);
  269. memcpy(buf, &v, sizeof(v));
  270. }
  271. static unsigned int regmap_parse_16_native(const void *buf)
  272. {
  273. u16 v;
  274. memcpy(&v, buf, sizeof(v));
  275. return v;
  276. }
  277. static unsigned int regmap_parse_24_be(const void *buf)
  278. {
  279. return get_unaligned_be24(buf);
  280. }
  281. static unsigned int regmap_parse_32_be(const void *buf)
  282. {
  283. return get_unaligned_be32(buf);
  284. }
  285. static unsigned int regmap_parse_32_le(const void *buf)
  286. {
  287. return get_unaligned_le32(buf);
  288. }
  289. static void regmap_parse_32_be_inplace(void *buf)
  290. {
  291. u32 v = get_unaligned_be32(buf);
  292. memcpy(buf, &v, sizeof(v));
  293. }
  294. static void regmap_parse_32_le_inplace(void *buf)
  295. {
  296. u32 v = get_unaligned_le32(buf);
  297. memcpy(buf, &v, sizeof(v));
  298. }
  299. static unsigned int regmap_parse_32_native(const void *buf)
  300. {
  301. u32 v;
  302. memcpy(&v, buf, sizeof(v));
  303. return v;
  304. }
  305. static void regmap_lock_hwlock(void *__map)
  306. {
  307. struct regmap *map = __map;
  308. hwspin_lock_timeout(map->hwlock, UINT_MAX);
  309. }
  310. static void regmap_lock_hwlock_irq(void *__map)
  311. {
  312. struct regmap *map = __map;
  313. hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
  314. }
  315. static void regmap_lock_hwlock_irqsave(void *__map)
  316. {
  317. struct regmap *map = __map;
  318. hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
  319. &map->spinlock_flags);
  320. }
  321. static void regmap_unlock_hwlock(void *__map)
  322. {
  323. struct regmap *map = __map;
  324. hwspin_unlock(map->hwlock);
  325. }
  326. static void regmap_unlock_hwlock_irq(void *__map)
  327. {
  328. struct regmap *map = __map;
  329. hwspin_unlock_irq(map->hwlock);
  330. }
  331. static void regmap_unlock_hwlock_irqrestore(void *__map)
  332. {
  333. struct regmap *map = __map;
  334. hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
  335. }
  336. static void regmap_lock_unlock_none(void *__map)
  337. {
  338. }
  339. static void regmap_lock_mutex(void *__map)
  340. {
  341. struct regmap *map = __map;
  342. mutex_lock(&map->mutex);
  343. }
  344. static void regmap_unlock_mutex(void *__map)
  345. {
  346. struct regmap *map = __map;
  347. mutex_unlock(&map->mutex);
  348. }
  349. static void regmap_lock_spinlock(void *__map)
  350. __acquires(&map->spinlock)
  351. {
  352. struct regmap *map = __map;
  353. unsigned long flags;
  354. spin_lock_irqsave(&map->spinlock, flags);
  355. map->spinlock_flags = flags;
  356. }
  357. static void regmap_unlock_spinlock(void *__map)
  358. __releases(&map->spinlock)
  359. {
  360. struct regmap *map = __map;
  361. spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
  362. }
  363. static void regmap_lock_raw_spinlock(void *__map)
  364. __acquires(&map->raw_spinlock)
  365. {
  366. struct regmap *map = __map;
  367. unsigned long flags;
  368. raw_spin_lock_irqsave(&map->raw_spinlock, flags);
  369. map->raw_spinlock_flags = flags;
  370. }
  371. static void regmap_unlock_raw_spinlock(void *__map)
  372. __releases(&map->raw_spinlock)
  373. {
  374. struct regmap *map = __map;
  375. raw_spin_unlock_irqrestore(&map->raw_spinlock, map->raw_spinlock_flags);
  376. }
  377. static void dev_get_regmap_release(struct device *dev, void *res)
  378. {
  379. /*
  380. * We don't actually have anything to do here; the goal here
  381. * is not to manage the regmap but to provide a simple way to
  382. * get the regmap back given a struct device.
  383. */
  384. }
  385. static bool _regmap_range_add(struct regmap *map,
  386. struct regmap_range_node *data)
  387. {
  388. struct rb_root *root = &map->range_tree;
  389. struct rb_node **new = &(root->rb_node), *parent = NULL;
  390. while (*new) {
  391. struct regmap_range_node *this =
  392. rb_entry(*new, struct regmap_range_node, node);
  393. parent = *new;
  394. if (data->range_max < this->range_min)
  395. new = &((*new)->rb_left);
  396. else if (data->range_min > this->range_max)
  397. new = &((*new)->rb_right);
  398. else
  399. return false;
  400. }
  401. rb_link_node(&data->node, parent, new);
  402. rb_insert_color(&data->node, root);
  403. return true;
  404. }
  405. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  406. unsigned int reg)
  407. {
  408. struct rb_node *node = map->range_tree.rb_node;
  409. while (node) {
  410. struct regmap_range_node *this =
  411. rb_entry(node, struct regmap_range_node, node);
  412. if (reg < this->range_min)
  413. node = node->rb_left;
  414. else if (reg > this->range_max)
  415. node = node->rb_right;
  416. else
  417. return this;
  418. }
  419. return NULL;
  420. }
  421. static void regmap_range_exit(struct regmap *map)
  422. {
  423. struct rb_node *next;
  424. struct regmap_range_node *range_node;
  425. next = rb_first(&map->range_tree);
  426. while (next) {
  427. range_node = rb_entry(next, struct regmap_range_node, node);
  428. next = rb_next(&range_node->node);
  429. rb_erase(&range_node->node, &map->range_tree);
  430. kfree(range_node);
  431. }
  432. kfree(map->selector_work_buf);
  433. }
  434. static int regmap_set_name(struct regmap *map, const struct regmap_config *config)
  435. {
  436. if (config->name) {
  437. const char *name = kstrdup_const(config->name, GFP_KERNEL);
  438. if (!name)
  439. return -ENOMEM;
  440. kfree_const(map->name);
  441. map->name = name;
  442. }
  443. return 0;
  444. }
  445. int regmap_attach_dev(struct device *dev, struct regmap *map,
  446. const struct regmap_config *config)
  447. {
  448. struct regmap **m;
  449. int ret;
  450. map->dev = dev;
  451. ret = regmap_set_name(map, config);
  452. if (ret)
  453. return ret;
  454. regmap_debugfs_exit(map);
  455. regmap_debugfs_init(map);
  456. /* Add a devres resource for dev_get_regmap() */
  457. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  458. if (!m) {
  459. regmap_debugfs_exit(map);
  460. return -ENOMEM;
  461. }
  462. *m = map;
  463. devres_add(dev, m);
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(regmap_attach_dev);
  467. static int dev_get_regmap_match(struct device *dev, void *res, void *data);
  468. static int regmap_detach_dev(struct device *dev, struct regmap *map)
  469. {
  470. if (!dev)
  471. return 0;
  472. return devres_release(dev, dev_get_regmap_release,
  473. dev_get_regmap_match, (void *)map->name);
  474. }
  475. static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
  476. const struct regmap_config *config)
  477. {
  478. enum regmap_endian endian;
  479. /* Retrieve the endianness specification from the regmap config */
  480. endian = config->reg_format_endian;
  481. /* If the regmap config specified a non-default value, use that */
  482. if (endian != REGMAP_ENDIAN_DEFAULT)
  483. return endian;
  484. /* Retrieve the endianness specification from the bus config */
  485. if (bus && bus->reg_format_endian_default)
  486. endian = bus->reg_format_endian_default;
  487. /* If the bus specified a non-default value, use that */
  488. if (endian != REGMAP_ENDIAN_DEFAULT)
  489. return endian;
  490. /* Use this if no other value was found */
  491. return REGMAP_ENDIAN_BIG;
  492. }
  493. enum regmap_endian regmap_get_val_endian(struct device *dev,
  494. const struct regmap_bus *bus,
  495. const struct regmap_config *config)
  496. {
  497. struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
  498. enum regmap_endian endian;
  499. /* Retrieve the endianness specification from the regmap config */
  500. endian = config->val_format_endian;
  501. /* If the regmap config specified a non-default value, use that */
  502. if (endian != REGMAP_ENDIAN_DEFAULT)
  503. return endian;
  504. /* If the firmware node exist try to get endianness from it */
  505. if (fwnode_property_read_bool(fwnode, "big-endian"))
  506. endian = REGMAP_ENDIAN_BIG;
  507. else if (fwnode_property_read_bool(fwnode, "little-endian"))
  508. endian = REGMAP_ENDIAN_LITTLE;
  509. else if (fwnode_property_read_bool(fwnode, "native-endian"))
  510. endian = REGMAP_ENDIAN_NATIVE;
  511. /* If the endianness was specified in fwnode, use that */
  512. if (endian != REGMAP_ENDIAN_DEFAULT)
  513. return endian;
  514. /* Retrieve the endianness specification from the bus config */
  515. if (bus && bus->val_format_endian_default)
  516. endian = bus->val_format_endian_default;
  517. /* If the bus specified a non-default value, use that */
  518. if (endian != REGMAP_ENDIAN_DEFAULT)
  519. return endian;
  520. /* Use this if no other value was found */
  521. return REGMAP_ENDIAN_BIG;
  522. }
  523. EXPORT_SYMBOL_GPL(regmap_get_val_endian);
  524. struct regmap *__regmap_init(struct device *dev,
  525. const struct regmap_bus *bus,
  526. void *bus_context,
  527. const struct regmap_config *config,
  528. struct lock_class_key *lock_key,
  529. const char *lock_name)
  530. {
  531. struct regmap *map;
  532. int ret = -EINVAL;
  533. enum regmap_endian reg_endian, val_endian;
  534. int i, j;
  535. if (!config)
  536. goto err;
  537. map = kzalloc(sizeof(*map), GFP_KERNEL);
  538. if (map == NULL) {
  539. ret = -ENOMEM;
  540. goto err;
  541. }
  542. ret = regmap_set_name(map, config);
  543. if (ret)
  544. goto err_map;
  545. ret = -EINVAL; /* Later error paths rely on this */
  546. if (config->disable_locking) {
  547. map->lock = map->unlock = regmap_lock_unlock_none;
  548. map->can_sleep = config->can_sleep;
  549. regmap_debugfs_disable(map);
  550. } else if (config->lock && config->unlock) {
  551. map->lock = config->lock;
  552. map->unlock = config->unlock;
  553. map->lock_arg = config->lock_arg;
  554. map->can_sleep = config->can_sleep;
  555. } else if (config->use_hwlock) {
  556. map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
  557. if (!map->hwlock) {
  558. ret = -ENXIO;
  559. goto err_name;
  560. }
  561. switch (config->hwlock_mode) {
  562. case HWLOCK_IRQSTATE:
  563. map->lock = regmap_lock_hwlock_irqsave;
  564. map->unlock = regmap_unlock_hwlock_irqrestore;
  565. break;
  566. case HWLOCK_IRQ:
  567. map->lock = regmap_lock_hwlock_irq;
  568. map->unlock = regmap_unlock_hwlock_irq;
  569. break;
  570. default:
  571. map->lock = regmap_lock_hwlock;
  572. map->unlock = regmap_unlock_hwlock;
  573. break;
  574. }
  575. map->lock_arg = map;
  576. } else {
  577. if ((bus && bus->fast_io) ||
  578. config->fast_io) {
  579. if (config->use_raw_spinlock) {
  580. raw_spin_lock_init(&map->raw_spinlock);
  581. map->lock = regmap_lock_raw_spinlock;
  582. map->unlock = regmap_unlock_raw_spinlock;
  583. lockdep_set_class_and_name(&map->raw_spinlock,
  584. lock_key, lock_name);
  585. } else {
  586. spin_lock_init(&map->spinlock);
  587. map->lock = regmap_lock_spinlock;
  588. map->unlock = regmap_unlock_spinlock;
  589. lockdep_set_class_and_name(&map->spinlock,
  590. lock_key, lock_name);
  591. }
  592. } else {
  593. mutex_init(&map->mutex);
  594. map->lock = regmap_lock_mutex;
  595. map->unlock = regmap_unlock_mutex;
  596. map->can_sleep = true;
  597. lockdep_set_class_and_name(&map->mutex,
  598. lock_key, lock_name);
  599. }
  600. map->lock_arg = map;
  601. map->lock_key = lock_key;
  602. }
  603. /*
  604. * When we write in fast-paths with regmap_bulk_write() don't allocate
  605. * scratch buffers with sleeping allocations.
  606. */
  607. if ((bus && bus->fast_io) || config->fast_io)
  608. map->alloc_flags = GFP_ATOMIC;
  609. else
  610. map->alloc_flags = GFP_KERNEL;
  611. map->reg_base = config->reg_base;
  612. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  613. map->format.pad_bytes = config->pad_bits / 8;
  614. map->format.reg_shift = config->reg_shift;
  615. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  616. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  617. config->val_bits + config->pad_bits, 8);
  618. map->reg_shift = config->pad_bits % 8;
  619. if (config->reg_stride)
  620. map->reg_stride = config->reg_stride;
  621. else
  622. map->reg_stride = 1;
  623. if (is_power_of_2(map->reg_stride))
  624. map->reg_stride_order = ilog2(map->reg_stride);
  625. else
  626. map->reg_stride_order = -1;
  627. map->use_single_read = config->use_single_read || !(config->read || (bus && bus->read));
  628. map->use_single_write = config->use_single_write || !(config->write || (bus && bus->write));
  629. map->can_multi_write = config->can_multi_write && (config->write || (bus && bus->write));
  630. if (bus) {
  631. map->max_raw_read = bus->max_raw_read;
  632. map->max_raw_write = bus->max_raw_write;
  633. } else if (config->max_raw_read && config->max_raw_write) {
  634. map->max_raw_read = config->max_raw_read;
  635. map->max_raw_write = config->max_raw_write;
  636. }
  637. map->dev = dev;
  638. map->bus = bus;
  639. map->bus_context = bus_context;
  640. map->max_register = config->max_register;
  641. map->max_register_is_set = map->max_register ?: config->max_register_is_0;
  642. map->wr_table = config->wr_table;
  643. map->rd_table = config->rd_table;
  644. map->volatile_table = config->volatile_table;
  645. map->precious_table = config->precious_table;
  646. map->wr_noinc_table = config->wr_noinc_table;
  647. map->rd_noinc_table = config->rd_noinc_table;
  648. map->writeable_reg = config->writeable_reg;
  649. map->readable_reg = config->readable_reg;
  650. map->volatile_reg = config->volatile_reg;
  651. map->precious_reg = config->precious_reg;
  652. map->writeable_noinc_reg = config->writeable_noinc_reg;
  653. map->readable_noinc_reg = config->readable_noinc_reg;
  654. map->cache_type = config->cache_type;
  655. spin_lock_init(&map->async_lock);
  656. INIT_LIST_HEAD(&map->async_list);
  657. INIT_LIST_HEAD(&map->async_free);
  658. init_waitqueue_head(&map->async_waitq);
  659. if (config->read_flag_mask ||
  660. config->write_flag_mask ||
  661. config->zero_flag_mask) {
  662. map->read_flag_mask = config->read_flag_mask;
  663. map->write_flag_mask = config->write_flag_mask;
  664. } else if (bus) {
  665. map->read_flag_mask = bus->read_flag_mask;
  666. }
  667. if (config->read && config->write) {
  668. map->reg_read = _regmap_bus_read;
  669. if (config->reg_update_bits)
  670. map->reg_update_bits = config->reg_update_bits;
  671. /* Bulk read/write */
  672. map->read = config->read;
  673. map->write = config->write;
  674. reg_endian = REGMAP_ENDIAN_NATIVE;
  675. val_endian = REGMAP_ENDIAN_NATIVE;
  676. } else if (!bus) {
  677. map->reg_read = config->reg_read;
  678. map->reg_write = config->reg_write;
  679. map->reg_update_bits = config->reg_update_bits;
  680. map->defer_caching = false;
  681. goto skip_format_initialization;
  682. } else if (!bus->read || !bus->write) {
  683. map->reg_read = _regmap_bus_reg_read;
  684. map->reg_write = _regmap_bus_reg_write;
  685. map->reg_update_bits = bus->reg_update_bits;
  686. map->defer_caching = false;
  687. goto skip_format_initialization;
  688. } else {
  689. map->reg_read = _regmap_bus_read;
  690. map->reg_update_bits = bus->reg_update_bits;
  691. /* Bulk read/write */
  692. map->read = bus->read;
  693. map->write = bus->write;
  694. reg_endian = regmap_get_reg_endian(bus, config);
  695. val_endian = regmap_get_val_endian(dev, bus, config);
  696. }
  697. switch (config->reg_bits + map->reg_shift) {
  698. case 2:
  699. switch (config->val_bits) {
  700. case 6:
  701. map->format.format_write = regmap_format_2_6_write;
  702. break;
  703. default:
  704. goto err_hwlock;
  705. }
  706. break;
  707. case 4:
  708. switch (config->val_bits) {
  709. case 12:
  710. map->format.format_write = regmap_format_4_12_write;
  711. break;
  712. default:
  713. goto err_hwlock;
  714. }
  715. break;
  716. case 7:
  717. switch (config->val_bits) {
  718. case 9:
  719. map->format.format_write = regmap_format_7_9_write;
  720. break;
  721. case 17:
  722. map->format.format_write = regmap_format_7_17_write;
  723. break;
  724. default:
  725. goto err_hwlock;
  726. }
  727. break;
  728. case 10:
  729. switch (config->val_bits) {
  730. case 14:
  731. map->format.format_write = regmap_format_10_14_write;
  732. break;
  733. default:
  734. goto err_hwlock;
  735. }
  736. break;
  737. case 12:
  738. switch (config->val_bits) {
  739. case 20:
  740. map->format.format_write = regmap_format_12_20_write;
  741. break;
  742. default:
  743. goto err_hwlock;
  744. }
  745. break;
  746. case 8:
  747. map->format.format_reg = regmap_format_8;
  748. break;
  749. case 16:
  750. switch (reg_endian) {
  751. case REGMAP_ENDIAN_BIG:
  752. map->format.format_reg = regmap_format_16_be;
  753. break;
  754. case REGMAP_ENDIAN_LITTLE:
  755. map->format.format_reg = regmap_format_16_le;
  756. break;
  757. case REGMAP_ENDIAN_NATIVE:
  758. map->format.format_reg = regmap_format_16_native;
  759. break;
  760. default:
  761. goto err_hwlock;
  762. }
  763. break;
  764. case 24:
  765. switch (reg_endian) {
  766. case REGMAP_ENDIAN_BIG:
  767. map->format.format_reg = regmap_format_24_be;
  768. break;
  769. default:
  770. goto err_hwlock;
  771. }
  772. break;
  773. case 32:
  774. switch (reg_endian) {
  775. case REGMAP_ENDIAN_BIG:
  776. map->format.format_reg = regmap_format_32_be;
  777. break;
  778. case REGMAP_ENDIAN_LITTLE:
  779. map->format.format_reg = regmap_format_32_le;
  780. break;
  781. case REGMAP_ENDIAN_NATIVE:
  782. map->format.format_reg = regmap_format_32_native;
  783. break;
  784. default:
  785. goto err_hwlock;
  786. }
  787. break;
  788. default:
  789. goto err_hwlock;
  790. }
  791. if (val_endian == REGMAP_ENDIAN_NATIVE)
  792. map->format.parse_inplace = regmap_parse_inplace_noop;
  793. switch (config->val_bits) {
  794. case 8:
  795. map->format.format_val = regmap_format_8;
  796. map->format.parse_val = regmap_parse_8;
  797. map->format.parse_inplace = regmap_parse_inplace_noop;
  798. break;
  799. case 16:
  800. switch (val_endian) {
  801. case REGMAP_ENDIAN_BIG:
  802. map->format.format_val = regmap_format_16_be;
  803. map->format.parse_val = regmap_parse_16_be;
  804. map->format.parse_inplace = regmap_parse_16_be_inplace;
  805. break;
  806. case REGMAP_ENDIAN_LITTLE:
  807. map->format.format_val = regmap_format_16_le;
  808. map->format.parse_val = regmap_parse_16_le;
  809. map->format.parse_inplace = regmap_parse_16_le_inplace;
  810. break;
  811. case REGMAP_ENDIAN_NATIVE:
  812. map->format.format_val = regmap_format_16_native;
  813. map->format.parse_val = regmap_parse_16_native;
  814. break;
  815. default:
  816. goto err_hwlock;
  817. }
  818. break;
  819. case 24:
  820. switch (val_endian) {
  821. case REGMAP_ENDIAN_BIG:
  822. map->format.format_val = regmap_format_24_be;
  823. map->format.parse_val = regmap_parse_24_be;
  824. break;
  825. default:
  826. goto err_hwlock;
  827. }
  828. break;
  829. case 32:
  830. switch (val_endian) {
  831. case REGMAP_ENDIAN_BIG:
  832. map->format.format_val = regmap_format_32_be;
  833. map->format.parse_val = regmap_parse_32_be;
  834. map->format.parse_inplace = regmap_parse_32_be_inplace;
  835. break;
  836. case REGMAP_ENDIAN_LITTLE:
  837. map->format.format_val = regmap_format_32_le;
  838. map->format.parse_val = regmap_parse_32_le;
  839. map->format.parse_inplace = regmap_parse_32_le_inplace;
  840. break;
  841. case REGMAP_ENDIAN_NATIVE:
  842. map->format.format_val = regmap_format_32_native;
  843. map->format.parse_val = regmap_parse_32_native;
  844. break;
  845. default:
  846. goto err_hwlock;
  847. }
  848. break;
  849. }
  850. if (map->format.format_write) {
  851. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  852. (val_endian != REGMAP_ENDIAN_BIG))
  853. goto err_hwlock;
  854. map->use_single_write = true;
  855. }
  856. if (!map->format.format_write &&
  857. !(map->format.format_reg && map->format.format_val))
  858. goto err_hwlock;
  859. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  860. if (map->work_buf == NULL) {
  861. ret = -ENOMEM;
  862. goto err_hwlock;
  863. }
  864. if (map->format.format_write) {
  865. map->defer_caching = false;
  866. map->reg_write = _regmap_bus_formatted_write;
  867. } else if (map->format.format_val) {
  868. map->defer_caching = true;
  869. map->reg_write = _regmap_bus_raw_write;
  870. }
  871. skip_format_initialization:
  872. map->range_tree = RB_ROOT;
  873. for (i = 0; i < config->num_ranges; i++) {
  874. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  875. struct regmap_range_node *new;
  876. /* Sanity check */
  877. if (range_cfg->range_max < range_cfg->range_min) {
  878. dev_err(map->dev, "Invalid range %d: %u < %u\n", i,
  879. range_cfg->range_max, range_cfg->range_min);
  880. goto err_range;
  881. }
  882. if (range_cfg->range_max > map->max_register) {
  883. dev_err(map->dev, "Invalid range %d: %u > %u\n", i,
  884. range_cfg->range_max, map->max_register);
  885. goto err_range;
  886. }
  887. if (range_cfg->selector_reg > map->max_register) {
  888. dev_err(map->dev,
  889. "Invalid range %d: selector out of map\n", i);
  890. goto err_range;
  891. }
  892. if (range_cfg->window_len == 0) {
  893. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  894. i);
  895. goto err_range;
  896. }
  897. /* Make sure, that this register range has no selector
  898. or data window within its boundary */
  899. for (j = 0; j < config->num_ranges; j++) {
  900. unsigned int sel_reg = config->ranges[j].selector_reg;
  901. unsigned int win_min = config->ranges[j].window_start;
  902. unsigned int win_max = win_min +
  903. config->ranges[j].window_len - 1;
  904. /* Allow data window inside its own virtual range */
  905. if (j == i)
  906. continue;
  907. if (range_cfg->range_min <= sel_reg &&
  908. sel_reg <= range_cfg->range_max) {
  909. dev_err(map->dev,
  910. "Range %d: selector for %d in window\n",
  911. i, j);
  912. goto err_range;
  913. }
  914. if (!(win_max < range_cfg->range_min ||
  915. win_min > range_cfg->range_max)) {
  916. dev_err(map->dev,
  917. "Range %d: window for %d in window\n",
  918. i, j);
  919. goto err_range;
  920. }
  921. }
  922. new = kzalloc(sizeof(*new), GFP_KERNEL);
  923. if (new == NULL) {
  924. ret = -ENOMEM;
  925. goto err_range;
  926. }
  927. new->map = map;
  928. new->name = range_cfg->name;
  929. new->range_min = range_cfg->range_min;
  930. new->range_max = range_cfg->range_max;
  931. new->selector_reg = range_cfg->selector_reg;
  932. new->selector_mask = range_cfg->selector_mask;
  933. new->selector_shift = range_cfg->selector_shift;
  934. new->window_start = range_cfg->window_start;
  935. new->window_len = range_cfg->window_len;
  936. if (!_regmap_range_add(map, new)) {
  937. dev_err(map->dev, "Failed to add range %d\n", i);
  938. kfree(new);
  939. goto err_range;
  940. }
  941. if (map->selector_work_buf == NULL) {
  942. map->selector_work_buf =
  943. kzalloc(map->format.buf_size, GFP_KERNEL);
  944. if (map->selector_work_buf == NULL) {
  945. ret = -ENOMEM;
  946. goto err_range;
  947. }
  948. }
  949. }
  950. ret = regcache_init(map, config);
  951. if (ret != 0)
  952. goto err_range;
  953. if (dev) {
  954. ret = regmap_attach_dev(dev, map, config);
  955. if (ret != 0)
  956. goto err_regcache;
  957. } else {
  958. regmap_debugfs_init(map);
  959. }
  960. return map;
  961. err_regcache:
  962. regcache_exit(map);
  963. err_range:
  964. regmap_range_exit(map);
  965. kfree(map->work_buf);
  966. err_hwlock:
  967. if (map->hwlock)
  968. hwspin_lock_free(map->hwlock);
  969. err_name:
  970. kfree_const(map->name);
  971. err_map:
  972. kfree(map);
  973. err:
  974. if (bus && bus->free_on_exit)
  975. kfree(bus);
  976. return ERR_PTR(ret);
  977. }
  978. EXPORT_SYMBOL_GPL(__regmap_init);
  979. static void devm_regmap_release(struct device *dev, void *res)
  980. {
  981. regmap_exit(*(struct regmap **)res);
  982. }
  983. struct regmap *__devm_regmap_init(struct device *dev,
  984. const struct regmap_bus *bus,
  985. void *bus_context,
  986. const struct regmap_config *config,
  987. struct lock_class_key *lock_key,
  988. const char *lock_name)
  989. {
  990. struct regmap **ptr, *regmap;
  991. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  992. if (!ptr)
  993. return ERR_PTR(-ENOMEM);
  994. regmap = __regmap_init(dev, bus, bus_context, config,
  995. lock_key, lock_name);
  996. if (!IS_ERR(regmap)) {
  997. *ptr = regmap;
  998. devres_add(dev, ptr);
  999. } else {
  1000. devres_free(ptr);
  1001. }
  1002. return regmap;
  1003. }
  1004. EXPORT_SYMBOL_GPL(__devm_regmap_init);
  1005. static void regmap_field_init(struct regmap_field *rm_field,
  1006. struct regmap *regmap, struct reg_field reg_field)
  1007. {
  1008. rm_field->regmap = regmap;
  1009. rm_field->reg = reg_field.reg;
  1010. rm_field->shift = reg_field.lsb;
  1011. rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
  1012. WARN_ONCE(rm_field->mask == 0, "invalid empty mask defined\n");
  1013. rm_field->id_size = reg_field.id_size;
  1014. rm_field->id_offset = reg_field.id_offset;
  1015. }
  1016. /**
  1017. * devm_regmap_field_alloc() - Allocate and initialise a register field.
  1018. *
  1019. * @dev: Device that will be interacted with
  1020. * @regmap: regmap bank in which this register field is located.
  1021. * @reg_field: Register field with in the bank.
  1022. *
  1023. * The return value will be an ERR_PTR() on error or a valid pointer
  1024. * to a struct regmap_field. The regmap_field will be automatically freed
  1025. * by the device management code.
  1026. */
  1027. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  1028. struct regmap *regmap, struct reg_field reg_field)
  1029. {
  1030. struct regmap_field *rm_field = devm_kzalloc(dev,
  1031. sizeof(*rm_field), GFP_KERNEL);
  1032. if (!rm_field)
  1033. return ERR_PTR(-ENOMEM);
  1034. regmap_field_init(rm_field, regmap, reg_field);
  1035. return rm_field;
  1036. }
  1037. EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
  1038. /**
  1039. * regmap_field_bulk_alloc() - Allocate and initialise a bulk register field.
  1040. *
  1041. * @regmap: regmap bank in which this register field is located.
  1042. * @rm_field: regmap register fields within the bank.
  1043. * @reg_field: Register fields within the bank.
  1044. * @num_fields: Number of register fields.
  1045. *
  1046. * The return value will be an -ENOMEM on error or zero for success.
  1047. * Newly allocated regmap_fields should be freed by calling
  1048. * regmap_field_bulk_free()
  1049. */
  1050. int regmap_field_bulk_alloc(struct regmap *regmap,
  1051. struct regmap_field **rm_field,
  1052. const struct reg_field *reg_field,
  1053. int num_fields)
  1054. {
  1055. struct regmap_field *rf;
  1056. int i;
  1057. rf = kcalloc(num_fields, sizeof(*rf), GFP_KERNEL);
  1058. if (!rf)
  1059. return -ENOMEM;
  1060. for (i = 0; i < num_fields; i++) {
  1061. regmap_field_init(&rf[i], regmap, reg_field[i]);
  1062. rm_field[i] = &rf[i];
  1063. }
  1064. return 0;
  1065. }
  1066. EXPORT_SYMBOL_GPL(regmap_field_bulk_alloc);
  1067. /**
  1068. * devm_regmap_field_bulk_alloc() - Allocate and initialise a bulk register
  1069. * fields.
  1070. *
  1071. * @dev: Device that will be interacted with
  1072. * @regmap: regmap bank in which this register field is located.
  1073. * @rm_field: regmap register fields within the bank.
  1074. * @reg_field: Register fields within the bank.
  1075. * @num_fields: Number of register fields.
  1076. *
  1077. * The return value will be an -ENOMEM on error or zero for success.
  1078. * Newly allocated regmap_fields will be automatically freed by the
  1079. * device management code.
  1080. */
  1081. int devm_regmap_field_bulk_alloc(struct device *dev,
  1082. struct regmap *regmap,
  1083. struct regmap_field **rm_field,
  1084. const struct reg_field *reg_field,
  1085. int num_fields)
  1086. {
  1087. struct regmap_field *rf;
  1088. int i;
  1089. rf = devm_kcalloc(dev, num_fields, sizeof(*rf), GFP_KERNEL);
  1090. if (!rf)
  1091. return -ENOMEM;
  1092. for (i = 0; i < num_fields; i++) {
  1093. regmap_field_init(&rf[i], regmap, reg_field[i]);
  1094. rm_field[i] = &rf[i];
  1095. }
  1096. return 0;
  1097. }
  1098. EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_alloc);
  1099. /**
  1100. * regmap_field_bulk_free() - Free register field allocated using
  1101. * regmap_field_bulk_alloc.
  1102. *
  1103. * @field: regmap fields which should be freed.
  1104. */
  1105. void regmap_field_bulk_free(struct regmap_field *field)
  1106. {
  1107. kfree(field);
  1108. }
  1109. EXPORT_SYMBOL_GPL(regmap_field_bulk_free);
  1110. /**
  1111. * devm_regmap_field_bulk_free() - Free a bulk register field allocated using
  1112. * devm_regmap_field_bulk_alloc.
  1113. *
  1114. * @dev: Device that will be interacted with
  1115. * @field: regmap field which should be freed.
  1116. *
  1117. * Free register field allocated using devm_regmap_field_bulk_alloc(). Usually
  1118. * drivers need not call this function, as the memory allocated via devm
  1119. * will be freed as per device-driver life-cycle.
  1120. */
  1121. void devm_regmap_field_bulk_free(struct device *dev,
  1122. struct regmap_field *field)
  1123. {
  1124. devm_kfree(dev, field);
  1125. }
  1126. EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_free);
  1127. /**
  1128. * devm_regmap_field_free() - Free a register field allocated using
  1129. * devm_regmap_field_alloc.
  1130. *
  1131. * @dev: Device that will be interacted with
  1132. * @field: regmap field which should be freed.
  1133. *
  1134. * Free register field allocated using devm_regmap_field_alloc(). Usually
  1135. * drivers need not call this function, as the memory allocated via devm
  1136. * will be freed as per device-driver life-cyle.
  1137. */
  1138. void devm_regmap_field_free(struct device *dev,
  1139. struct regmap_field *field)
  1140. {
  1141. devm_kfree(dev, field);
  1142. }
  1143. EXPORT_SYMBOL_GPL(devm_regmap_field_free);
  1144. /**
  1145. * regmap_field_alloc() - Allocate and initialise a register field.
  1146. *
  1147. * @regmap: regmap bank in which this register field is located.
  1148. * @reg_field: Register field with in the bank.
  1149. *
  1150. * The return value will be an ERR_PTR() on error or a valid pointer
  1151. * to a struct regmap_field. The regmap_field should be freed by the
  1152. * user once its finished working with it using regmap_field_free().
  1153. */
  1154. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  1155. struct reg_field reg_field)
  1156. {
  1157. struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
  1158. if (!rm_field)
  1159. return ERR_PTR(-ENOMEM);
  1160. regmap_field_init(rm_field, regmap, reg_field);
  1161. return rm_field;
  1162. }
  1163. EXPORT_SYMBOL_GPL(regmap_field_alloc);
  1164. /**
  1165. * regmap_field_free() - Free register field allocated using
  1166. * regmap_field_alloc.
  1167. *
  1168. * @field: regmap field which should be freed.
  1169. */
  1170. void regmap_field_free(struct regmap_field *field)
  1171. {
  1172. kfree(field);
  1173. }
  1174. EXPORT_SYMBOL_GPL(regmap_field_free);
  1175. /**
  1176. * regmap_reinit_cache() - Reinitialise the current register cache
  1177. *
  1178. * @map: Register map to operate on.
  1179. * @config: New configuration. Only the cache data will be used.
  1180. *
  1181. * Discard any existing register cache for the map and initialize a
  1182. * new cache. This can be used to restore the cache to defaults or to
  1183. * update the cache configuration to reflect runtime discovery of the
  1184. * hardware.
  1185. *
  1186. * No explicit locking is done here, the user needs to ensure that
  1187. * this function will not race with other calls to regmap.
  1188. */
  1189. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  1190. {
  1191. int ret;
  1192. regcache_exit(map);
  1193. regmap_debugfs_exit(map);
  1194. map->max_register = config->max_register;
  1195. map->max_register_is_set = map->max_register ?: config->max_register_is_0;
  1196. map->writeable_reg = config->writeable_reg;
  1197. map->readable_reg = config->readable_reg;
  1198. map->volatile_reg = config->volatile_reg;
  1199. map->precious_reg = config->precious_reg;
  1200. map->writeable_noinc_reg = config->writeable_noinc_reg;
  1201. map->readable_noinc_reg = config->readable_noinc_reg;
  1202. map->cache_type = config->cache_type;
  1203. ret = regmap_set_name(map, config);
  1204. if (ret)
  1205. return ret;
  1206. regmap_debugfs_init(map);
  1207. map->cache_bypass = false;
  1208. map->cache_only = false;
  1209. return regcache_init(map, config);
  1210. }
  1211. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  1212. /**
  1213. * regmap_exit() - Free a previously allocated register map
  1214. *
  1215. * @map: Register map to operate on.
  1216. */
  1217. void regmap_exit(struct regmap *map)
  1218. {
  1219. struct regmap_async *async;
  1220. regmap_detach_dev(map->dev, map);
  1221. regcache_exit(map);
  1222. regmap_debugfs_exit(map);
  1223. regmap_range_exit(map);
  1224. if (map->bus && map->bus->free_context)
  1225. map->bus->free_context(map->bus_context);
  1226. kfree(map->work_buf);
  1227. while (!list_empty(&map->async_free)) {
  1228. async = list_first_entry_or_null(&map->async_free,
  1229. struct regmap_async,
  1230. list);
  1231. list_del(&async->list);
  1232. kfree(async->work_buf);
  1233. kfree(async);
  1234. }
  1235. if (map->hwlock)
  1236. hwspin_lock_free(map->hwlock);
  1237. if (map->lock == regmap_lock_mutex)
  1238. mutex_destroy(&map->mutex);
  1239. kfree_const(map->name);
  1240. kfree(map->patch);
  1241. if (map->bus && map->bus->free_on_exit)
  1242. kfree(map->bus);
  1243. kfree(map);
  1244. }
  1245. EXPORT_SYMBOL_GPL(regmap_exit);
  1246. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  1247. {
  1248. struct regmap **r = res;
  1249. if (!r || !*r) {
  1250. WARN_ON(!r || !*r);
  1251. return 0;
  1252. }
  1253. /* If the user didn't specify a name match any */
  1254. if (data)
  1255. return (*r)->name && !strcmp((*r)->name, data);
  1256. else
  1257. return 1;
  1258. }
  1259. /**
  1260. * dev_get_regmap() - Obtain the regmap (if any) for a device
  1261. *
  1262. * @dev: Device to retrieve the map for
  1263. * @name: Optional name for the register map, usually NULL.
  1264. *
  1265. * Returns the regmap for the device if one is present, or NULL. If
  1266. * name is specified then it must match the name specified when
  1267. * registering the device, if it is NULL then the first regmap found
  1268. * will be used. Devices with multiple register maps are very rare,
  1269. * generic code should normally not need to specify a name.
  1270. */
  1271. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  1272. {
  1273. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  1274. dev_get_regmap_match, (void *)name);
  1275. if (!r)
  1276. return NULL;
  1277. return *r;
  1278. }
  1279. EXPORT_SYMBOL_GPL(dev_get_regmap);
  1280. /**
  1281. * regmap_get_device() - Obtain the device from a regmap
  1282. *
  1283. * @map: Register map to operate on.
  1284. *
  1285. * Returns the underlying device that the regmap has been created for.
  1286. */
  1287. struct device *regmap_get_device(struct regmap *map)
  1288. {
  1289. return map->dev;
  1290. }
  1291. EXPORT_SYMBOL_GPL(regmap_get_device);
  1292. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  1293. struct regmap_range_node *range,
  1294. unsigned int val_num)
  1295. {
  1296. void *orig_work_buf;
  1297. unsigned int win_offset;
  1298. unsigned int win_page;
  1299. bool page_chg;
  1300. int ret;
  1301. win_offset = (*reg - range->range_min) % range->window_len;
  1302. win_page = (*reg - range->range_min) / range->window_len;
  1303. if (val_num > 1) {
  1304. /* Bulk write shouldn't cross range boundary */
  1305. if (*reg + val_num - 1 > range->range_max)
  1306. return -EINVAL;
  1307. /* ... or single page boundary */
  1308. if (val_num > range->window_len - win_offset)
  1309. return -EINVAL;
  1310. }
  1311. /* It is possible to have selector register inside data window.
  1312. In that case, selector register is located on every page and
  1313. it needs no page switching, when accessed alone. */
  1314. if (val_num > 1 ||
  1315. range->window_start + win_offset != range->selector_reg) {
  1316. /* Use separate work_buf during page switching */
  1317. orig_work_buf = map->work_buf;
  1318. map->work_buf = map->selector_work_buf;
  1319. ret = _regmap_update_bits(map, range->selector_reg,
  1320. range->selector_mask,
  1321. win_page << range->selector_shift,
  1322. &page_chg, false);
  1323. map->work_buf = orig_work_buf;
  1324. if (ret != 0)
  1325. return ret;
  1326. }
  1327. *reg = range->window_start + win_offset;
  1328. return 0;
  1329. }
  1330. static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
  1331. unsigned long mask)
  1332. {
  1333. u8 *buf;
  1334. int i;
  1335. if (!mask || !map->work_buf)
  1336. return;
  1337. buf = map->work_buf;
  1338. for (i = 0; i < max_bytes; i++)
  1339. buf[i] |= (mask >> (8 * i)) & 0xff;
  1340. }
  1341. static unsigned int regmap_reg_addr(struct regmap *map, unsigned int reg)
  1342. {
  1343. reg += map->reg_base;
  1344. if (map->format.reg_shift > 0)
  1345. reg >>= map->format.reg_shift;
  1346. else if (map->format.reg_shift < 0)
  1347. reg <<= -(map->format.reg_shift);
  1348. return reg;
  1349. }
  1350. static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
  1351. const void *val, size_t val_len, bool noinc)
  1352. {
  1353. struct regmap_range_node *range;
  1354. unsigned long flags;
  1355. void *work_val = map->work_buf + map->format.reg_bytes +
  1356. map->format.pad_bytes;
  1357. void *buf;
  1358. int ret = -ENOTSUPP;
  1359. size_t len;
  1360. int i;
  1361. /* Check for unwritable or noinc registers in range
  1362. * before we start
  1363. */
  1364. if (!regmap_writeable_noinc(map, reg)) {
  1365. for (i = 0; i < val_len / map->format.val_bytes; i++) {
  1366. unsigned int element =
  1367. reg + regmap_get_offset(map, i);
  1368. if (!regmap_writeable(map, element) ||
  1369. regmap_writeable_noinc(map, element))
  1370. return -EINVAL;
  1371. }
  1372. }
  1373. if (!map->cache_bypass && map->format.parse_val) {
  1374. unsigned int ival, offset;
  1375. int val_bytes = map->format.val_bytes;
  1376. /* Cache the last written value for noinc writes */
  1377. i = noinc ? val_len - val_bytes : 0;
  1378. for (; i < val_len; i += val_bytes) {
  1379. ival = map->format.parse_val(val + i);
  1380. offset = noinc ? 0 : regmap_get_offset(map, i / val_bytes);
  1381. ret = regcache_write(map, reg + offset, ival);
  1382. if (ret) {
  1383. dev_err(map->dev,
  1384. "Error in caching of register: %x ret: %d\n",
  1385. reg + offset, ret);
  1386. return ret;
  1387. }
  1388. }
  1389. if (map->cache_only) {
  1390. map->cache_dirty = true;
  1391. return 0;
  1392. }
  1393. }
  1394. range = _regmap_range_lookup(map, reg);
  1395. if (range) {
  1396. int val_num = val_len / map->format.val_bytes;
  1397. int win_offset = (reg - range->range_min) % range->window_len;
  1398. int win_residue = range->window_len - win_offset;
  1399. /* If the write goes beyond the end of the window split it */
  1400. while (val_num > win_residue) {
  1401. dev_dbg(map->dev, "Writing window %d/%zu\n",
  1402. win_residue, val_len / map->format.val_bytes);
  1403. ret = _regmap_raw_write_impl(map, reg, val,
  1404. win_residue *
  1405. map->format.val_bytes, noinc);
  1406. if (ret != 0)
  1407. return ret;
  1408. reg += win_residue;
  1409. val_num -= win_residue;
  1410. val += win_residue * map->format.val_bytes;
  1411. val_len -= win_residue * map->format.val_bytes;
  1412. win_offset = (reg - range->range_min) %
  1413. range->window_len;
  1414. win_residue = range->window_len - win_offset;
  1415. }
  1416. ret = _regmap_select_page(map, &reg, range, noinc ? 1 : val_num);
  1417. if (ret != 0)
  1418. return ret;
  1419. }
  1420. reg = regmap_reg_addr(map, reg);
  1421. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1422. regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
  1423. map->write_flag_mask);
  1424. /*
  1425. * Essentially all I/O mechanisms will be faster with a single
  1426. * buffer to write. Since register syncs often generate raw
  1427. * writes of single registers optimise that case.
  1428. */
  1429. if (val != work_val && val_len == map->format.val_bytes) {
  1430. memcpy(work_val, val, map->format.val_bytes);
  1431. val = work_val;
  1432. }
  1433. if (map->async && map->bus && map->bus->async_write) {
  1434. struct regmap_async *async;
  1435. trace_regmap_async_write_start(map, reg, val_len);
  1436. spin_lock_irqsave(&map->async_lock, flags);
  1437. async = list_first_entry_or_null(&map->async_free,
  1438. struct regmap_async,
  1439. list);
  1440. if (async)
  1441. list_del(&async->list);
  1442. spin_unlock_irqrestore(&map->async_lock, flags);
  1443. if (!async) {
  1444. async = map->bus->async_alloc();
  1445. if (!async)
  1446. return -ENOMEM;
  1447. async->work_buf = kzalloc(map->format.buf_size,
  1448. GFP_KERNEL | GFP_DMA);
  1449. if (!async->work_buf) {
  1450. kfree(async);
  1451. return -ENOMEM;
  1452. }
  1453. }
  1454. async->map = map;
  1455. /* If the caller supplied the value we can use it safely. */
  1456. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  1457. map->format.reg_bytes + map->format.val_bytes);
  1458. spin_lock_irqsave(&map->async_lock, flags);
  1459. list_add_tail(&async->list, &map->async_list);
  1460. spin_unlock_irqrestore(&map->async_lock, flags);
  1461. if (val != work_val)
  1462. ret = map->bus->async_write(map->bus_context,
  1463. async->work_buf,
  1464. map->format.reg_bytes +
  1465. map->format.pad_bytes,
  1466. val, val_len, async);
  1467. else
  1468. ret = map->bus->async_write(map->bus_context,
  1469. async->work_buf,
  1470. map->format.reg_bytes +
  1471. map->format.pad_bytes +
  1472. val_len, NULL, 0, async);
  1473. if (ret != 0) {
  1474. dev_err(map->dev, "Failed to schedule write: %d\n",
  1475. ret);
  1476. spin_lock_irqsave(&map->async_lock, flags);
  1477. list_move(&async->list, &map->async_free);
  1478. spin_unlock_irqrestore(&map->async_lock, flags);
  1479. }
  1480. return ret;
  1481. }
  1482. trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
  1483. /* If we're doing a single register write we can probably just
  1484. * send the work_buf directly, otherwise try to do a gather
  1485. * write.
  1486. */
  1487. if (val == work_val)
  1488. ret = map->write(map->bus_context, map->work_buf,
  1489. map->format.reg_bytes +
  1490. map->format.pad_bytes +
  1491. val_len);
  1492. else if (map->bus && map->bus->gather_write)
  1493. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  1494. map->format.reg_bytes +
  1495. map->format.pad_bytes,
  1496. val, val_len);
  1497. else
  1498. ret = -ENOTSUPP;
  1499. /* If that didn't work fall back on linearising by hand. */
  1500. if (ret == -ENOTSUPP) {
  1501. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  1502. buf = kzalloc(len, GFP_KERNEL);
  1503. if (!buf)
  1504. return -ENOMEM;
  1505. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1506. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1507. val, val_len);
  1508. ret = map->write(map->bus_context, buf, len);
  1509. kfree(buf);
  1510. } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
  1511. /* regcache_drop_region() takes lock that we already have,
  1512. * thus call map->cache_ops->drop() directly
  1513. */
  1514. if (map->cache_ops && map->cache_ops->drop)
  1515. map->cache_ops->drop(map, reg, reg + 1);
  1516. }
  1517. trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
  1518. return ret;
  1519. }
  1520. /**
  1521. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1522. *
  1523. * @map: Map to check.
  1524. */
  1525. bool regmap_can_raw_write(struct regmap *map)
  1526. {
  1527. return map->write && map->format.format_val && map->format.format_reg;
  1528. }
  1529. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1530. /**
  1531. * regmap_get_raw_read_max - Get the maximum size we can read
  1532. *
  1533. * @map: Map to check.
  1534. */
  1535. size_t regmap_get_raw_read_max(struct regmap *map)
  1536. {
  1537. return map->max_raw_read;
  1538. }
  1539. EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
  1540. /**
  1541. * regmap_get_raw_write_max - Get the maximum size we can read
  1542. *
  1543. * @map: Map to check.
  1544. */
  1545. size_t regmap_get_raw_write_max(struct regmap *map)
  1546. {
  1547. return map->max_raw_write;
  1548. }
  1549. EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
  1550. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1551. unsigned int val)
  1552. {
  1553. int ret;
  1554. struct regmap_range_node *range;
  1555. struct regmap *map = context;
  1556. WARN_ON(!map->format.format_write);
  1557. range = _regmap_range_lookup(map, reg);
  1558. if (range) {
  1559. ret = _regmap_select_page(map, &reg, range, 1);
  1560. if (ret != 0)
  1561. return ret;
  1562. }
  1563. reg = regmap_reg_addr(map, reg);
  1564. map->format.format_write(map, reg, val);
  1565. trace_regmap_hw_write_start(map, reg, 1);
  1566. ret = map->write(map->bus_context, map->work_buf, map->format.buf_size);
  1567. trace_regmap_hw_write_done(map, reg, 1);
  1568. return ret;
  1569. }
  1570. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  1571. unsigned int val)
  1572. {
  1573. struct regmap *map = context;
  1574. struct regmap_range_node *range;
  1575. int ret;
  1576. range = _regmap_range_lookup(map, reg);
  1577. if (range) {
  1578. ret = _regmap_select_page(map, &reg, range, 1);
  1579. if (ret != 0)
  1580. return ret;
  1581. }
  1582. reg = regmap_reg_addr(map, reg);
  1583. return map->bus->reg_write(map->bus_context, reg, val);
  1584. }
  1585. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1586. unsigned int val)
  1587. {
  1588. struct regmap *map = context;
  1589. WARN_ON(!map->format.format_val);
  1590. map->format.format_val(map->work_buf + map->format.reg_bytes
  1591. + map->format.pad_bytes, val, 0);
  1592. return _regmap_raw_write_impl(map, reg,
  1593. map->work_buf +
  1594. map->format.reg_bytes +
  1595. map->format.pad_bytes,
  1596. map->format.val_bytes,
  1597. false);
  1598. }
  1599. static inline void *_regmap_map_get_context(struct regmap *map)
  1600. {
  1601. return (map->bus || (!map->bus && map->read)) ? map : map->bus_context;
  1602. }
  1603. int _regmap_write(struct regmap *map, unsigned int reg,
  1604. unsigned int val)
  1605. {
  1606. int ret;
  1607. void *context = _regmap_map_get_context(map);
  1608. if (!regmap_writeable(map, reg))
  1609. return -EIO;
  1610. if (!map->cache_bypass && !map->defer_caching) {
  1611. ret = regcache_write(map, reg, val);
  1612. if (ret != 0)
  1613. return ret;
  1614. if (map->cache_only) {
  1615. map->cache_dirty = true;
  1616. return 0;
  1617. }
  1618. }
  1619. ret = map->reg_write(context, reg, val);
  1620. if (ret == 0) {
  1621. if (regmap_should_log(map))
  1622. dev_info(map->dev, "%x <= %x\n", reg, val);
  1623. trace_regmap_reg_write(map, reg, val);
  1624. }
  1625. return ret;
  1626. }
  1627. /**
  1628. * regmap_write() - Write a value to a single register
  1629. *
  1630. * @map: Register map to write to
  1631. * @reg: Register to write to
  1632. * @val: Value to be written
  1633. *
  1634. * A value of zero will be returned on success, a negative errno will
  1635. * be returned in error cases.
  1636. */
  1637. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1638. {
  1639. int ret;
  1640. if (!IS_ALIGNED(reg, map->reg_stride))
  1641. return -EINVAL;
  1642. map->lock(map->lock_arg);
  1643. ret = _regmap_write(map, reg, val);
  1644. map->unlock(map->lock_arg);
  1645. return ret;
  1646. }
  1647. EXPORT_SYMBOL_GPL(regmap_write);
  1648. /**
  1649. * regmap_write_async() - Write a value to a single register asynchronously
  1650. *
  1651. * @map: Register map to write to
  1652. * @reg: Register to write to
  1653. * @val: Value to be written
  1654. *
  1655. * A value of zero will be returned on success, a negative errno will
  1656. * be returned in error cases.
  1657. */
  1658. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
  1659. {
  1660. int ret;
  1661. if (!IS_ALIGNED(reg, map->reg_stride))
  1662. return -EINVAL;
  1663. map->lock(map->lock_arg);
  1664. map->async = true;
  1665. ret = _regmap_write(map, reg, val);
  1666. map->async = false;
  1667. map->unlock(map->lock_arg);
  1668. return ret;
  1669. }
  1670. EXPORT_SYMBOL_GPL(regmap_write_async);
  1671. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  1672. const void *val, size_t val_len, bool noinc)
  1673. {
  1674. size_t val_bytes = map->format.val_bytes;
  1675. size_t val_count = val_len / val_bytes;
  1676. size_t chunk_count, chunk_bytes;
  1677. size_t chunk_regs = val_count;
  1678. int ret, i;
  1679. if (!val_count)
  1680. return -EINVAL;
  1681. if (map->use_single_write)
  1682. chunk_regs = 1;
  1683. else if (map->max_raw_write && val_len > map->max_raw_write)
  1684. chunk_regs = map->max_raw_write / val_bytes;
  1685. chunk_count = val_count / chunk_regs;
  1686. chunk_bytes = chunk_regs * val_bytes;
  1687. /* Write as many bytes as possible with chunk_size */
  1688. for (i = 0; i < chunk_count; i++) {
  1689. ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes, noinc);
  1690. if (ret)
  1691. return ret;
  1692. reg += regmap_get_offset(map, chunk_regs);
  1693. val += chunk_bytes;
  1694. val_len -= chunk_bytes;
  1695. }
  1696. /* Write remaining bytes */
  1697. if (val_len)
  1698. ret = _regmap_raw_write_impl(map, reg, val, val_len, noinc);
  1699. return ret;
  1700. }
  1701. /**
  1702. * regmap_raw_write() - Write raw values to one or more registers
  1703. *
  1704. * @map: Register map to write to
  1705. * @reg: Initial register to write to
  1706. * @val: Block of data to be written, laid out for direct transmission to the
  1707. * device
  1708. * @val_len: Length of data pointed to by val.
  1709. *
  1710. * This function is intended to be used for things like firmware
  1711. * download where a large block of data needs to be transferred to the
  1712. * device. No formatting will be done on the data provided.
  1713. *
  1714. * A value of zero will be returned on success, a negative errno will
  1715. * be returned in error cases.
  1716. */
  1717. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1718. const void *val, size_t val_len)
  1719. {
  1720. int ret;
  1721. if (!regmap_can_raw_write(map))
  1722. return -EINVAL;
  1723. if (val_len % map->format.val_bytes)
  1724. return -EINVAL;
  1725. map->lock(map->lock_arg);
  1726. ret = _regmap_raw_write(map, reg, val, val_len, false);
  1727. map->unlock(map->lock_arg);
  1728. return ret;
  1729. }
  1730. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1731. static int regmap_noinc_readwrite(struct regmap *map, unsigned int reg,
  1732. void *val, unsigned int val_len, bool write)
  1733. {
  1734. size_t val_bytes = map->format.val_bytes;
  1735. size_t val_count = val_len / val_bytes;
  1736. unsigned int lastval;
  1737. u8 *u8p;
  1738. u16 *u16p;
  1739. u32 *u32p;
  1740. int ret;
  1741. int i;
  1742. switch (val_bytes) {
  1743. case 1:
  1744. u8p = val;
  1745. if (write)
  1746. lastval = (unsigned int)u8p[val_count - 1];
  1747. break;
  1748. case 2:
  1749. u16p = val;
  1750. if (write)
  1751. lastval = (unsigned int)u16p[val_count - 1];
  1752. break;
  1753. case 4:
  1754. u32p = val;
  1755. if (write)
  1756. lastval = (unsigned int)u32p[val_count - 1];
  1757. break;
  1758. default:
  1759. return -EINVAL;
  1760. }
  1761. /*
  1762. * Update the cache with the last value we write, the rest is just
  1763. * gone down in the hardware FIFO. We can't cache FIFOs. This makes
  1764. * sure a single read from the cache will work.
  1765. */
  1766. if (write) {
  1767. if (!map->cache_bypass && !map->defer_caching) {
  1768. ret = regcache_write(map, reg, lastval);
  1769. if (ret != 0)
  1770. return ret;
  1771. if (map->cache_only) {
  1772. map->cache_dirty = true;
  1773. return 0;
  1774. }
  1775. }
  1776. ret = map->bus->reg_noinc_write(map->bus_context, reg, val, val_count);
  1777. } else {
  1778. ret = map->bus->reg_noinc_read(map->bus_context, reg, val, val_count);
  1779. }
  1780. if (!ret && regmap_should_log(map)) {
  1781. dev_info(map->dev, "%x %s [", reg, write ? "<=" : "=>");
  1782. for (i = 0; i < val_count; i++) {
  1783. switch (val_bytes) {
  1784. case 1:
  1785. pr_cont("%x", u8p[i]);
  1786. break;
  1787. case 2:
  1788. pr_cont("%x", u16p[i]);
  1789. break;
  1790. case 4:
  1791. pr_cont("%x", u32p[i]);
  1792. break;
  1793. default:
  1794. break;
  1795. }
  1796. if (i == (val_count - 1))
  1797. pr_cont("]\n");
  1798. else
  1799. pr_cont(",");
  1800. }
  1801. }
  1802. return 0;
  1803. }
  1804. /**
  1805. * regmap_noinc_write(): Write data to a register without incrementing the
  1806. * register number
  1807. *
  1808. * @map: Register map to write to
  1809. * @reg: Register to write to
  1810. * @val: Pointer to data buffer
  1811. * @val_len: Length of output buffer in bytes.
  1812. *
  1813. * The regmap API usually assumes that bulk bus write operations will write a
  1814. * range of registers. Some devices have certain registers for which a write
  1815. * operation can write to an internal FIFO.
  1816. *
  1817. * The target register must be volatile but registers after it can be
  1818. * completely unrelated cacheable registers.
  1819. *
  1820. * This will attempt multiple writes as required to write val_len bytes.
  1821. *
  1822. * A value of zero will be returned on success, a negative errno will be
  1823. * returned in error cases.
  1824. */
  1825. int regmap_noinc_write(struct regmap *map, unsigned int reg,
  1826. const void *val, size_t val_len)
  1827. {
  1828. size_t write_len;
  1829. int ret;
  1830. if (!map->write && !(map->bus && map->bus->reg_noinc_write))
  1831. return -EINVAL;
  1832. if (val_len % map->format.val_bytes)
  1833. return -EINVAL;
  1834. if (!IS_ALIGNED(reg, map->reg_stride))
  1835. return -EINVAL;
  1836. if (val_len == 0)
  1837. return -EINVAL;
  1838. map->lock(map->lock_arg);
  1839. if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) {
  1840. ret = -EINVAL;
  1841. goto out_unlock;
  1842. }
  1843. /*
  1844. * Use the accelerated operation if we can. The val drops the const
  1845. * typing in order to facilitate code reuse in regmap_noinc_readwrite().
  1846. */
  1847. if (map->bus->reg_noinc_write) {
  1848. ret = regmap_noinc_readwrite(map, reg, (void *)val, val_len, true);
  1849. goto out_unlock;
  1850. }
  1851. while (val_len) {
  1852. if (map->max_raw_write && map->max_raw_write < val_len)
  1853. write_len = map->max_raw_write;
  1854. else
  1855. write_len = val_len;
  1856. ret = _regmap_raw_write(map, reg, val, write_len, true);
  1857. if (ret)
  1858. goto out_unlock;
  1859. val = ((u8 *)val) + write_len;
  1860. val_len -= write_len;
  1861. }
  1862. out_unlock:
  1863. map->unlock(map->lock_arg);
  1864. return ret;
  1865. }
  1866. EXPORT_SYMBOL_GPL(regmap_noinc_write);
  1867. /**
  1868. * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
  1869. * register field.
  1870. *
  1871. * @field: Register field to write to
  1872. * @mask: Bitmask to change
  1873. * @val: Value to be written
  1874. * @change: Boolean indicating if a write was done
  1875. * @async: Boolean indicating asynchronously
  1876. * @force: Boolean indicating use force update
  1877. *
  1878. * Perform a read/modify/write cycle on the register field with change,
  1879. * async, force option.
  1880. *
  1881. * A value of zero will be returned on success, a negative errno will
  1882. * be returned in error cases.
  1883. */
  1884. int regmap_field_update_bits_base(struct regmap_field *field,
  1885. unsigned int mask, unsigned int val,
  1886. bool *change, bool async, bool force)
  1887. {
  1888. mask = (mask << field->shift) & field->mask;
  1889. return regmap_update_bits_base(field->regmap, field->reg,
  1890. mask, val << field->shift,
  1891. change, async, force);
  1892. }
  1893. EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
  1894. /**
  1895. * regmap_field_test_bits() - Check if all specified bits are set in a
  1896. * register field.
  1897. *
  1898. * @field: Register field to operate on
  1899. * @bits: Bits to test
  1900. *
  1901. * Returns -1 if the underlying regmap_field_read() fails, 0 if at least one of the
  1902. * tested bits is not set and 1 if all tested bits are set.
  1903. */
  1904. int regmap_field_test_bits(struct regmap_field *field, unsigned int bits)
  1905. {
  1906. unsigned int val, ret;
  1907. ret = regmap_field_read(field, &val);
  1908. if (ret)
  1909. return ret;
  1910. return (val & bits) == bits;
  1911. }
  1912. EXPORT_SYMBOL_GPL(regmap_field_test_bits);
  1913. /**
  1914. * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
  1915. * register field with port ID
  1916. *
  1917. * @field: Register field to write to
  1918. * @id: port ID
  1919. * @mask: Bitmask to change
  1920. * @val: Value to be written
  1921. * @change: Boolean indicating if a write was done
  1922. * @async: Boolean indicating asynchronously
  1923. * @force: Boolean indicating use force update
  1924. *
  1925. * A value of zero will be returned on success, a negative errno will
  1926. * be returned in error cases.
  1927. */
  1928. int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
  1929. unsigned int mask, unsigned int val,
  1930. bool *change, bool async, bool force)
  1931. {
  1932. if (id >= field->id_size)
  1933. return -EINVAL;
  1934. mask = (mask << field->shift) & field->mask;
  1935. return regmap_update_bits_base(field->regmap,
  1936. field->reg + (field->id_offset * id),
  1937. mask, val << field->shift,
  1938. change, async, force);
  1939. }
  1940. EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
  1941. /**
  1942. * regmap_bulk_write() - Write multiple registers to the device
  1943. *
  1944. * @map: Register map to write to
  1945. * @reg: First register to be write from
  1946. * @val: Block of data to be written, in native register size for device
  1947. * @val_count: Number of registers to write
  1948. *
  1949. * This function is intended to be used for writing a large block of
  1950. * data to the device either in single transfer or multiple transfer.
  1951. *
  1952. * A value of zero will be returned on success, a negative errno will
  1953. * be returned in error cases.
  1954. */
  1955. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1956. size_t val_count)
  1957. {
  1958. int ret = 0, i;
  1959. size_t val_bytes = map->format.val_bytes;
  1960. if (!IS_ALIGNED(reg, map->reg_stride))
  1961. return -EINVAL;
  1962. /*
  1963. * Some devices don't support bulk write, for them we have a series of
  1964. * single write operations.
  1965. */
  1966. if (!map->write || !map->format.parse_inplace) {
  1967. map->lock(map->lock_arg);
  1968. for (i = 0; i < val_count; i++) {
  1969. unsigned int ival;
  1970. switch (val_bytes) {
  1971. case 1:
  1972. ival = *(u8 *)(val + (i * val_bytes));
  1973. break;
  1974. case 2:
  1975. ival = *(u16 *)(val + (i * val_bytes));
  1976. break;
  1977. case 4:
  1978. ival = *(u32 *)(val + (i * val_bytes));
  1979. break;
  1980. default:
  1981. ret = -EINVAL;
  1982. goto out;
  1983. }
  1984. ret = _regmap_write(map,
  1985. reg + regmap_get_offset(map, i),
  1986. ival);
  1987. if (ret != 0)
  1988. goto out;
  1989. }
  1990. out:
  1991. map->unlock(map->lock_arg);
  1992. } else {
  1993. void *wval;
  1994. wval = kmemdup_array(val, val_count, val_bytes, map->alloc_flags);
  1995. if (!wval)
  1996. return -ENOMEM;
  1997. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1998. map->format.parse_inplace(wval + i);
  1999. ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
  2000. kfree(wval);
  2001. }
  2002. if (!ret)
  2003. trace_regmap_bulk_write(map, reg, val, val_bytes * val_count);
  2004. return ret;
  2005. }
  2006. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  2007. /*
  2008. * _regmap_raw_multi_reg_write()
  2009. *
  2010. * the (register,newvalue) pairs in regs have not been formatted, but
  2011. * they are all in the same page and have been changed to being page
  2012. * relative. The page register has been written if that was necessary.
  2013. */
  2014. static int _regmap_raw_multi_reg_write(struct regmap *map,
  2015. const struct reg_sequence *regs,
  2016. size_t num_regs)
  2017. {
  2018. int ret;
  2019. void *buf;
  2020. int i;
  2021. u8 *u8;
  2022. size_t val_bytes = map->format.val_bytes;
  2023. size_t reg_bytes = map->format.reg_bytes;
  2024. size_t pad_bytes = map->format.pad_bytes;
  2025. size_t pair_size = reg_bytes + pad_bytes + val_bytes;
  2026. size_t len = pair_size * num_regs;
  2027. if (!len)
  2028. return -EINVAL;
  2029. buf = kzalloc(len, GFP_KERNEL);
  2030. if (!buf)
  2031. return -ENOMEM;
  2032. /* We have to linearise by hand. */
  2033. u8 = buf;
  2034. for (i = 0; i < num_regs; i++) {
  2035. unsigned int reg = regs[i].reg;
  2036. unsigned int val = regs[i].def;
  2037. trace_regmap_hw_write_start(map, reg, 1);
  2038. reg = regmap_reg_addr(map, reg);
  2039. map->format.format_reg(u8, reg, map->reg_shift);
  2040. u8 += reg_bytes + pad_bytes;
  2041. map->format.format_val(u8, val, 0);
  2042. u8 += val_bytes;
  2043. }
  2044. u8 = buf;
  2045. *u8 |= map->write_flag_mask;
  2046. ret = map->write(map->bus_context, buf, len);
  2047. kfree(buf);
  2048. for (i = 0; i < num_regs; i++) {
  2049. int reg = regs[i].reg;
  2050. trace_regmap_hw_write_done(map, reg, 1);
  2051. }
  2052. return ret;
  2053. }
  2054. static unsigned int _regmap_register_page(struct regmap *map,
  2055. unsigned int reg,
  2056. struct regmap_range_node *range)
  2057. {
  2058. unsigned int win_page = (reg - range->range_min) / range->window_len;
  2059. return win_page;
  2060. }
  2061. static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  2062. struct reg_sequence *regs,
  2063. size_t num_regs)
  2064. {
  2065. int ret;
  2066. int i, n;
  2067. struct reg_sequence *base;
  2068. unsigned int this_page = 0;
  2069. unsigned int page_change = 0;
  2070. /*
  2071. * the set of registers are not neccessarily in order, but
  2072. * since the order of write must be preserved this algorithm
  2073. * chops the set each time the page changes. This also applies
  2074. * if there is a delay required at any point in the sequence.
  2075. */
  2076. base = regs;
  2077. for (i = 0, n = 0; i < num_regs; i++, n++) {
  2078. unsigned int reg = regs[i].reg;
  2079. struct regmap_range_node *range;
  2080. range = _regmap_range_lookup(map, reg);
  2081. if (range) {
  2082. unsigned int win_page = _regmap_register_page(map, reg,
  2083. range);
  2084. if (i == 0)
  2085. this_page = win_page;
  2086. if (win_page != this_page) {
  2087. this_page = win_page;
  2088. page_change = 1;
  2089. }
  2090. }
  2091. /* If we have both a page change and a delay make sure to
  2092. * write the regs and apply the delay before we change the
  2093. * page.
  2094. */
  2095. if (page_change || regs[i].delay_us) {
  2096. /* For situations where the first write requires
  2097. * a delay we need to make sure we don't call
  2098. * raw_multi_reg_write with n=0
  2099. * This can't occur with page breaks as we
  2100. * never write on the first iteration
  2101. */
  2102. if (regs[i].delay_us && i == 0)
  2103. n = 1;
  2104. ret = _regmap_raw_multi_reg_write(map, base, n);
  2105. if (ret != 0)
  2106. return ret;
  2107. if (regs[i].delay_us) {
  2108. if (map->can_sleep)
  2109. fsleep(regs[i].delay_us);
  2110. else
  2111. udelay(regs[i].delay_us);
  2112. }
  2113. base += n;
  2114. n = 0;
  2115. if (page_change) {
  2116. ret = _regmap_select_page(map,
  2117. &base[n].reg,
  2118. range, 1);
  2119. if (ret != 0)
  2120. return ret;
  2121. page_change = 0;
  2122. }
  2123. }
  2124. }
  2125. if (n > 0)
  2126. return _regmap_raw_multi_reg_write(map, base, n);
  2127. return 0;
  2128. }
  2129. static int _regmap_multi_reg_write(struct regmap *map,
  2130. const struct reg_sequence *regs,
  2131. size_t num_regs)
  2132. {
  2133. int i;
  2134. int ret;
  2135. if (!map->can_multi_write) {
  2136. for (i = 0; i < num_regs; i++) {
  2137. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  2138. if (ret != 0)
  2139. return ret;
  2140. if (regs[i].delay_us) {
  2141. if (map->can_sleep)
  2142. fsleep(regs[i].delay_us);
  2143. else
  2144. udelay(regs[i].delay_us);
  2145. }
  2146. }
  2147. return 0;
  2148. }
  2149. if (!map->format.parse_inplace)
  2150. return -EINVAL;
  2151. if (map->writeable_reg)
  2152. for (i = 0; i < num_regs; i++) {
  2153. int reg = regs[i].reg;
  2154. if (!map->writeable_reg(map->dev, reg))
  2155. return -EINVAL;
  2156. if (!IS_ALIGNED(reg, map->reg_stride))
  2157. return -EINVAL;
  2158. }
  2159. if (!map->cache_bypass) {
  2160. for (i = 0; i < num_regs; i++) {
  2161. unsigned int val = regs[i].def;
  2162. unsigned int reg = regs[i].reg;
  2163. ret = regcache_write(map, reg, val);
  2164. if (ret) {
  2165. dev_err(map->dev,
  2166. "Error in caching of register: %x ret: %d\n",
  2167. reg, ret);
  2168. return ret;
  2169. }
  2170. }
  2171. if (map->cache_only) {
  2172. map->cache_dirty = true;
  2173. return 0;
  2174. }
  2175. }
  2176. WARN_ON(!map->bus);
  2177. for (i = 0; i < num_regs; i++) {
  2178. unsigned int reg = regs[i].reg;
  2179. struct regmap_range_node *range;
  2180. /* Coalesce all the writes between a page break or a delay
  2181. * in a sequence
  2182. */
  2183. range = _regmap_range_lookup(map, reg);
  2184. if (range || regs[i].delay_us) {
  2185. size_t len = sizeof(struct reg_sequence)*num_regs;
  2186. struct reg_sequence *base = kmemdup(regs, len,
  2187. GFP_KERNEL);
  2188. if (!base)
  2189. return -ENOMEM;
  2190. ret = _regmap_range_multi_paged_reg_write(map, base,
  2191. num_regs);
  2192. kfree(base);
  2193. return ret;
  2194. }
  2195. }
  2196. return _regmap_raw_multi_reg_write(map, regs, num_regs);
  2197. }
  2198. /**
  2199. * regmap_multi_reg_write() - Write multiple registers to the device
  2200. *
  2201. * @map: Register map to write to
  2202. * @regs: Array of structures containing register,value to be written
  2203. * @num_regs: Number of registers to write
  2204. *
  2205. * Write multiple registers to the device where the set of register, value
  2206. * pairs are supplied in any order, possibly not all in a single range.
  2207. *
  2208. * The 'normal' block write mode will send ultimately send data on the
  2209. * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
  2210. * addressed. However, this alternative block multi write mode will send
  2211. * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
  2212. * must of course support the mode.
  2213. *
  2214. * A value of zero will be returned on success, a negative errno will be
  2215. * returned in error cases.
  2216. */
  2217. int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
  2218. int num_regs)
  2219. {
  2220. int ret;
  2221. map->lock(map->lock_arg);
  2222. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2223. map->unlock(map->lock_arg);
  2224. return ret;
  2225. }
  2226. EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
  2227. /**
  2228. * regmap_multi_reg_write_bypassed() - Write multiple registers to the
  2229. * device but not the cache
  2230. *
  2231. * @map: Register map to write to
  2232. * @regs: Array of structures containing register,value to be written
  2233. * @num_regs: Number of registers to write
  2234. *
  2235. * Write multiple registers to the device but not the cache where the set
  2236. * of register are supplied in any order.
  2237. *
  2238. * This function is intended to be used for writing a large block of data
  2239. * atomically to the device in single transfer for those I2C client devices
  2240. * that implement this alternative block write mode.
  2241. *
  2242. * A value of zero will be returned on success, a negative errno will
  2243. * be returned in error cases.
  2244. */
  2245. int regmap_multi_reg_write_bypassed(struct regmap *map,
  2246. const struct reg_sequence *regs,
  2247. int num_regs)
  2248. {
  2249. int ret;
  2250. bool bypass;
  2251. map->lock(map->lock_arg);
  2252. bypass = map->cache_bypass;
  2253. map->cache_bypass = true;
  2254. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2255. map->cache_bypass = bypass;
  2256. map->unlock(map->lock_arg);
  2257. return ret;
  2258. }
  2259. EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
  2260. /**
  2261. * regmap_raw_write_async() - Write raw values to one or more registers
  2262. * asynchronously
  2263. *
  2264. * @map: Register map to write to
  2265. * @reg: Initial register to write to
  2266. * @val: Block of data to be written, laid out for direct transmission to the
  2267. * device. Must be valid until regmap_async_complete() is called.
  2268. * @val_len: Length of data pointed to by val.
  2269. *
  2270. * This function is intended to be used for things like firmware
  2271. * download where a large block of data needs to be transferred to the
  2272. * device. No formatting will be done on the data provided.
  2273. *
  2274. * If supported by the underlying bus the write will be scheduled
  2275. * asynchronously, helping maximise I/O speed on higher speed buses
  2276. * like SPI. regmap_async_complete() can be called to ensure that all
  2277. * asynchrnous writes have been completed.
  2278. *
  2279. * A value of zero will be returned on success, a negative errno will
  2280. * be returned in error cases.
  2281. */
  2282. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  2283. const void *val, size_t val_len)
  2284. {
  2285. int ret;
  2286. if (val_len % map->format.val_bytes)
  2287. return -EINVAL;
  2288. if (!IS_ALIGNED(reg, map->reg_stride))
  2289. return -EINVAL;
  2290. map->lock(map->lock_arg);
  2291. map->async = true;
  2292. ret = _regmap_raw_write(map, reg, val, val_len, false);
  2293. map->async = false;
  2294. map->unlock(map->lock_arg);
  2295. return ret;
  2296. }
  2297. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  2298. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  2299. unsigned int val_len, bool noinc)
  2300. {
  2301. struct regmap_range_node *range;
  2302. int ret;
  2303. if (!map->read)
  2304. return -EINVAL;
  2305. range = _regmap_range_lookup(map, reg);
  2306. if (range) {
  2307. ret = _regmap_select_page(map, &reg, range,
  2308. noinc ? 1 : val_len / map->format.val_bytes);
  2309. if (ret != 0)
  2310. return ret;
  2311. }
  2312. reg = regmap_reg_addr(map, reg);
  2313. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  2314. regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
  2315. map->read_flag_mask);
  2316. trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
  2317. ret = map->read(map->bus_context, map->work_buf,
  2318. map->format.reg_bytes + map->format.pad_bytes,
  2319. val, val_len);
  2320. trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
  2321. return ret;
  2322. }
  2323. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  2324. unsigned int *val)
  2325. {
  2326. struct regmap *map = context;
  2327. struct regmap_range_node *range;
  2328. int ret;
  2329. range = _regmap_range_lookup(map, reg);
  2330. if (range) {
  2331. ret = _regmap_select_page(map, &reg, range, 1);
  2332. if (ret != 0)
  2333. return ret;
  2334. }
  2335. reg = regmap_reg_addr(map, reg);
  2336. return map->bus->reg_read(map->bus_context, reg, val);
  2337. }
  2338. static int _regmap_bus_read(void *context, unsigned int reg,
  2339. unsigned int *val)
  2340. {
  2341. int ret;
  2342. struct regmap *map = context;
  2343. void *work_val = map->work_buf + map->format.reg_bytes +
  2344. map->format.pad_bytes;
  2345. if (!map->format.parse_val)
  2346. return -EINVAL;
  2347. ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false);
  2348. if (ret == 0)
  2349. *val = map->format.parse_val(work_val);
  2350. return ret;
  2351. }
  2352. static int _regmap_read(struct regmap *map, unsigned int reg,
  2353. unsigned int *val)
  2354. {
  2355. int ret;
  2356. void *context = _regmap_map_get_context(map);
  2357. if (!map->cache_bypass) {
  2358. ret = regcache_read(map, reg, val);
  2359. if (ret == 0)
  2360. return 0;
  2361. }
  2362. if (map->cache_only)
  2363. return -EBUSY;
  2364. if (!regmap_readable(map, reg))
  2365. return -EIO;
  2366. ret = map->reg_read(context, reg, val);
  2367. if (ret == 0) {
  2368. if (regmap_should_log(map))
  2369. dev_info(map->dev, "%x => %x\n", reg, *val);
  2370. trace_regmap_reg_read(map, reg, *val);
  2371. if (!map->cache_bypass)
  2372. regcache_write(map, reg, *val);
  2373. }
  2374. return ret;
  2375. }
  2376. /**
  2377. * regmap_read() - Read a value from a single register
  2378. *
  2379. * @map: Register map to read from
  2380. * @reg: Register to be read from
  2381. * @val: Pointer to store read value
  2382. *
  2383. * A value of zero will be returned on success, a negative errno will
  2384. * be returned in error cases.
  2385. */
  2386. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  2387. {
  2388. int ret;
  2389. if (!IS_ALIGNED(reg, map->reg_stride))
  2390. return -EINVAL;
  2391. map->lock(map->lock_arg);
  2392. ret = _regmap_read(map, reg, val);
  2393. map->unlock(map->lock_arg);
  2394. return ret;
  2395. }
  2396. EXPORT_SYMBOL_GPL(regmap_read);
  2397. /**
  2398. * regmap_read_bypassed() - Read a value from a single register direct
  2399. * from the device, bypassing the cache
  2400. *
  2401. * @map: Register map to read from
  2402. * @reg: Register to be read from
  2403. * @val: Pointer to store read value
  2404. *
  2405. * A value of zero will be returned on success, a negative errno will
  2406. * be returned in error cases.
  2407. */
  2408. int regmap_read_bypassed(struct regmap *map, unsigned int reg, unsigned int *val)
  2409. {
  2410. int ret;
  2411. bool bypass, cache_only;
  2412. if (!IS_ALIGNED(reg, map->reg_stride))
  2413. return -EINVAL;
  2414. map->lock(map->lock_arg);
  2415. bypass = map->cache_bypass;
  2416. cache_only = map->cache_only;
  2417. map->cache_bypass = true;
  2418. map->cache_only = false;
  2419. ret = _regmap_read(map, reg, val);
  2420. map->cache_bypass = bypass;
  2421. map->cache_only = cache_only;
  2422. map->unlock(map->lock_arg);
  2423. return ret;
  2424. }
  2425. EXPORT_SYMBOL_GPL(regmap_read_bypassed);
  2426. /**
  2427. * regmap_raw_read() - Read raw data from the device
  2428. *
  2429. * @map: Register map to read from
  2430. * @reg: First register to be read from
  2431. * @val: Pointer to store read value
  2432. * @val_len: Size of data to read
  2433. *
  2434. * A value of zero will be returned on success, a negative errno will
  2435. * be returned in error cases.
  2436. */
  2437. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  2438. size_t val_len)
  2439. {
  2440. size_t val_bytes = map->format.val_bytes;
  2441. size_t val_count = val_len / val_bytes;
  2442. unsigned int v;
  2443. int ret, i;
  2444. if (val_len % map->format.val_bytes)
  2445. return -EINVAL;
  2446. if (!IS_ALIGNED(reg, map->reg_stride))
  2447. return -EINVAL;
  2448. if (val_count == 0)
  2449. return -EINVAL;
  2450. map->lock(map->lock_arg);
  2451. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  2452. map->cache_type == REGCACHE_NONE) {
  2453. size_t chunk_count, chunk_bytes;
  2454. size_t chunk_regs = val_count;
  2455. if (!map->cache_bypass && map->cache_only) {
  2456. ret = -EBUSY;
  2457. goto out;
  2458. }
  2459. if (!map->read) {
  2460. ret = -ENOTSUPP;
  2461. goto out;
  2462. }
  2463. if (map->use_single_read)
  2464. chunk_regs = 1;
  2465. else if (map->max_raw_read && val_len > map->max_raw_read)
  2466. chunk_regs = map->max_raw_read / val_bytes;
  2467. chunk_count = val_count / chunk_regs;
  2468. chunk_bytes = chunk_regs * val_bytes;
  2469. /* Read bytes that fit into whole chunks */
  2470. for (i = 0; i < chunk_count; i++) {
  2471. ret = _regmap_raw_read(map, reg, val, chunk_bytes, false);
  2472. if (ret != 0)
  2473. goto out;
  2474. reg += regmap_get_offset(map, chunk_regs);
  2475. val += chunk_bytes;
  2476. val_len -= chunk_bytes;
  2477. }
  2478. /* Read remaining bytes */
  2479. if (val_len) {
  2480. ret = _regmap_raw_read(map, reg, val, val_len, false);
  2481. if (ret != 0)
  2482. goto out;
  2483. }
  2484. } else {
  2485. /* Otherwise go word by word for the cache; should be low
  2486. * cost as we expect to hit the cache.
  2487. */
  2488. for (i = 0; i < val_count; i++) {
  2489. ret = _regmap_read(map, reg + regmap_get_offset(map, i),
  2490. &v);
  2491. if (ret != 0)
  2492. goto out;
  2493. map->format.format_val(val + (i * val_bytes), v, 0);
  2494. }
  2495. }
  2496. out:
  2497. map->unlock(map->lock_arg);
  2498. return ret;
  2499. }
  2500. EXPORT_SYMBOL_GPL(regmap_raw_read);
  2501. /**
  2502. * regmap_noinc_read(): Read data from a register without incrementing the
  2503. * register number
  2504. *
  2505. * @map: Register map to read from
  2506. * @reg: Register to read from
  2507. * @val: Pointer to data buffer
  2508. * @val_len: Length of output buffer in bytes.
  2509. *
  2510. * The regmap API usually assumes that bulk read operations will read a
  2511. * range of registers. Some devices have certain registers for which a read
  2512. * operation read will read from an internal FIFO.
  2513. *
  2514. * The target register must be volatile but registers after it can be
  2515. * completely unrelated cacheable registers.
  2516. *
  2517. * This will attempt multiple reads as required to read val_len bytes.
  2518. *
  2519. * A value of zero will be returned on success, a negative errno will be
  2520. * returned in error cases.
  2521. */
  2522. int regmap_noinc_read(struct regmap *map, unsigned int reg,
  2523. void *val, size_t val_len)
  2524. {
  2525. size_t read_len;
  2526. int ret;
  2527. if (!map->read)
  2528. return -ENOTSUPP;
  2529. if (val_len % map->format.val_bytes)
  2530. return -EINVAL;
  2531. if (!IS_ALIGNED(reg, map->reg_stride))
  2532. return -EINVAL;
  2533. if (val_len == 0)
  2534. return -EINVAL;
  2535. map->lock(map->lock_arg);
  2536. if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) {
  2537. ret = -EINVAL;
  2538. goto out_unlock;
  2539. }
  2540. /*
  2541. * We have not defined the FIFO semantics for cache, as the
  2542. * cache is just one value deep. Should we return the last
  2543. * written value? Just avoid this by always reading the FIFO
  2544. * even when using cache. Cache only will not work.
  2545. */
  2546. if (!map->cache_bypass && map->cache_only) {
  2547. ret = -EBUSY;
  2548. goto out_unlock;
  2549. }
  2550. /* Use the accelerated operation if we can */
  2551. if (map->bus->reg_noinc_read) {
  2552. ret = regmap_noinc_readwrite(map, reg, val, val_len, false);
  2553. goto out_unlock;
  2554. }
  2555. while (val_len) {
  2556. if (map->max_raw_read && map->max_raw_read < val_len)
  2557. read_len = map->max_raw_read;
  2558. else
  2559. read_len = val_len;
  2560. ret = _regmap_raw_read(map, reg, val, read_len, true);
  2561. if (ret)
  2562. goto out_unlock;
  2563. val = ((u8 *)val) + read_len;
  2564. val_len -= read_len;
  2565. }
  2566. out_unlock:
  2567. map->unlock(map->lock_arg);
  2568. return ret;
  2569. }
  2570. EXPORT_SYMBOL_GPL(regmap_noinc_read);
  2571. /**
  2572. * regmap_field_read(): Read a value to a single register field
  2573. *
  2574. * @field: Register field to read from
  2575. * @val: Pointer to store read value
  2576. *
  2577. * A value of zero will be returned on success, a negative errno will
  2578. * be returned in error cases.
  2579. */
  2580. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  2581. {
  2582. int ret;
  2583. unsigned int reg_val;
  2584. ret = regmap_read(field->regmap, field->reg, &reg_val);
  2585. if (ret != 0)
  2586. return ret;
  2587. reg_val &= field->mask;
  2588. reg_val >>= field->shift;
  2589. *val = reg_val;
  2590. return ret;
  2591. }
  2592. EXPORT_SYMBOL_GPL(regmap_field_read);
  2593. /**
  2594. * regmap_fields_read() - Read a value to a single register field with port ID
  2595. *
  2596. * @field: Register field to read from
  2597. * @id: port ID
  2598. * @val: Pointer to store read value
  2599. *
  2600. * A value of zero will be returned on success, a negative errno will
  2601. * be returned in error cases.
  2602. */
  2603. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  2604. unsigned int *val)
  2605. {
  2606. int ret;
  2607. unsigned int reg_val;
  2608. if (id >= field->id_size)
  2609. return -EINVAL;
  2610. ret = regmap_read(field->regmap,
  2611. field->reg + (field->id_offset * id),
  2612. &reg_val);
  2613. if (ret != 0)
  2614. return ret;
  2615. reg_val &= field->mask;
  2616. reg_val >>= field->shift;
  2617. *val = reg_val;
  2618. return ret;
  2619. }
  2620. EXPORT_SYMBOL_GPL(regmap_fields_read);
  2621. static int _regmap_bulk_read(struct regmap *map, unsigned int reg,
  2622. unsigned int *regs, void *val, size_t val_count)
  2623. {
  2624. u32 *u32 = val;
  2625. u16 *u16 = val;
  2626. u8 *u8 = val;
  2627. int ret, i;
  2628. map->lock(map->lock_arg);
  2629. for (i = 0; i < val_count; i++) {
  2630. unsigned int ival;
  2631. if (regs) {
  2632. if (!IS_ALIGNED(regs[i], map->reg_stride)) {
  2633. ret = -EINVAL;
  2634. goto out;
  2635. }
  2636. ret = _regmap_read(map, regs[i], &ival);
  2637. } else {
  2638. ret = _regmap_read(map, reg + regmap_get_offset(map, i), &ival);
  2639. }
  2640. if (ret != 0)
  2641. goto out;
  2642. switch (map->format.val_bytes) {
  2643. case 4:
  2644. u32[i] = ival;
  2645. break;
  2646. case 2:
  2647. u16[i] = ival;
  2648. break;
  2649. case 1:
  2650. u8[i] = ival;
  2651. break;
  2652. default:
  2653. ret = -EINVAL;
  2654. goto out;
  2655. }
  2656. }
  2657. out:
  2658. map->unlock(map->lock_arg);
  2659. return ret;
  2660. }
  2661. /**
  2662. * regmap_bulk_read() - Read multiple sequential registers from the device
  2663. *
  2664. * @map: Register map to read from
  2665. * @reg: First register to be read from
  2666. * @val: Pointer to store read value, in native register size for device
  2667. * @val_count: Number of registers to read
  2668. *
  2669. * A value of zero will be returned on success, a negative errno will
  2670. * be returned in error cases.
  2671. */
  2672. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  2673. size_t val_count)
  2674. {
  2675. int ret, i;
  2676. size_t val_bytes = map->format.val_bytes;
  2677. bool vol = regmap_volatile_range(map, reg, val_count);
  2678. if (!IS_ALIGNED(reg, map->reg_stride))
  2679. return -EINVAL;
  2680. if (val_count == 0)
  2681. return -EINVAL;
  2682. if (map->read && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
  2683. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  2684. if (ret != 0)
  2685. return ret;
  2686. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  2687. map->format.parse_inplace(val + i);
  2688. } else {
  2689. ret = _regmap_bulk_read(map, reg, NULL, val, val_count);
  2690. }
  2691. if (!ret)
  2692. trace_regmap_bulk_read(map, reg, val, val_bytes * val_count);
  2693. return ret;
  2694. }
  2695. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  2696. /**
  2697. * regmap_multi_reg_read() - Read multiple non-sequential registers from the device
  2698. *
  2699. * @map: Register map to read from
  2700. * @regs: Array of registers to read from
  2701. * @val: Pointer to store read value, in native register size for device
  2702. * @val_count: Number of registers to read
  2703. *
  2704. * A value of zero will be returned on success, a negative errno will
  2705. * be returned in error cases.
  2706. */
  2707. int regmap_multi_reg_read(struct regmap *map, unsigned int *regs, void *val,
  2708. size_t val_count)
  2709. {
  2710. if (val_count == 0)
  2711. return -EINVAL;
  2712. return _regmap_bulk_read(map, 0, regs, val, val_count);
  2713. }
  2714. EXPORT_SYMBOL_GPL(regmap_multi_reg_read);
  2715. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  2716. unsigned int mask, unsigned int val,
  2717. bool *change, bool force_write)
  2718. {
  2719. int ret;
  2720. unsigned int tmp, orig;
  2721. if (change)
  2722. *change = false;
  2723. if (regmap_volatile(map, reg) && map->reg_update_bits) {
  2724. reg = regmap_reg_addr(map, reg);
  2725. ret = map->reg_update_bits(map->bus_context, reg, mask, val);
  2726. if (ret == 0 && change)
  2727. *change = true;
  2728. } else {
  2729. ret = _regmap_read(map, reg, &orig);
  2730. if (ret != 0)
  2731. return ret;
  2732. tmp = orig & ~mask;
  2733. tmp |= val & mask;
  2734. if (force_write || (tmp != orig) || map->force_write_field) {
  2735. ret = _regmap_write(map, reg, tmp);
  2736. if (ret == 0 && change)
  2737. *change = true;
  2738. }
  2739. }
  2740. return ret;
  2741. }
  2742. /**
  2743. * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
  2744. *
  2745. * @map: Register map to update
  2746. * @reg: Register to update
  2747. * @mask: Bitmask to change
  2748. * @val: New value for bitmask
  2749. * @change: Boolean indicating if a write was done
  2750. * @async: Boolean indicating asynchronously
  2751. * @force: Boolean indicating use force update
  2752. *
  2753. * Perform a read/modify/write cycle on a register map with change, async, force
  2754. * options.
  2755. *
  2756. * If async is true:
  2757. *
  2758. * With most buses the read must be done synchronously so this is most useful
  2759. * for devices with a cache which do not need to interact with the hardware to
  2760. * determine the current register value.
  2761. *
  2762. * Returns zero for success, a negative number on error.
  2763. */
  2764. int regmap_update_bits_base(struct regmap *map, unsigned int reg,
  2765. unsigned int mask, unsigned int val,
  2766. bool *change, bool async, bool force)
  2767. {
  2768. int ret;
  2769. map->lock(map->lock_arg);
  2770. map->async = async;
  2771. ret = _regmap_update_bits(map, reg, mask, val, change, force);
  2772. map->async = false;
  2773. map->unlock(map->lock_arg);
  2774. return ret;
  2775. }
  2776. EXPORT_SYMBOL_GPL(regmap_update_bits_base);
  2777. /**
  2778. * regmap_test_bits() - Check if all specified bits are set in a register.
  2779. *
  2780. * @map: Register map to operate on
  2781. * @reg: Register to read from
  2782. * @bits: Bits to test
  2783. *
  2784. * Returns 0 if at least one of the tested bits is not set, 1 if all tested
  2785. * bits are set and a negative error number if the underlying regmap_read()
  2786. * fails.
  2787. */
  2788. int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits)
  2789. {
  2790. unsigned int val, ret;
  2791. ret = regmap_read(map, reg, &val);
  2792. if (ret)
  2793. return ret;
  2794. return (val & bits) == bits;
  2795. }
  2796. EXPORT_SYMBOL_GPL(regmap_test_bits);
  2797. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  2798. {
  2799. struct regmap *map = async->map;
  2800. bool wake;
  2801. trace_regmap_async_io_complete(map);
  2802. spin_lock(&map->async_lock);
  2803. list_move(&async->list, &map->async_free);
  2804. wake = list_empty(&map->async_list);
  2805. if (ret != 0)
  2806. map->async_ret = ret;
  2807. spin_unlock(&map->async_lock);
  2808. if (wake)
  2809. wake_up(&map->async_waitq);
  2810. }
  2811. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  2812. static int regmap_async_is_done(struct regmap *map)
  2813. {
  2814. unsigned long flags;
  2815. int ret;
  2816. spin_lock_irqsave(&map->async_lock, flags);
  2817. ret = list_empty(&map->async_list);
  2818. spin_unlock_irqrestore(&map->async_lock, flags);
  2819. return ret;
  2820. }
  2821. /**
  2822. * regmap_async_complete - Ensure all asynchronous I/O has completed.
  2823. *
  2824. * @map: Map to operate on.
  2825. *
  2826. * Blocks until any pending asynchronous I/O has completed. Returns
  2827. * an error code for any failed I/O operations.
  2828. */
  2829. int regmap_async_complete(struct regmap *map)
  2830. {
  2831. unsigned long flags;
  2832. int ret;
  2833. /* Nothing to do with no async support */
  2834. if (!map->bus || !map->bus->async_write)
  2835. return 0;
  2836. trace_regmap_async_complete_start(map);
  2837. wait_event(map->async_waitq, regmap_async_is_done(map));
  2838. spin_lock_irqsave(&map->async_lock, flags);
  2839. ret = map->async_ret;
  2840. map->async_ret = 0;
  2841. spin_unlock_irqrestore(&map->async_lock, flags);
  2842. trace_regmap_async_complete_done(map);
  2843. return ret;
  2844. }
  2845. EXPORT_SYMBOL_GPL(regmap_async_complete);
  2846. /**
  2847. * regmap_register_patch - Register and apply register updates to be applied
  2848. * on device initialistion
  2849. *
  2850. * @map: Register map to apply updates to.
  2851. * @regs: Values to update.
  2852. * @num_regs: Number of entries in regs.
  2853. *
  2854. * Register a set of register updates to be applied to the device
  2855. * whenever the device registers are synchronised with the cache and
  2856. * apply them immediately. Typically this is used to apply
  2857. * corrections to be applied to the device defaults on startup, such
  2858. * as the updates some vendors provide to undocumented registers.
  2859. *
  2860. * The caller must ensure that this function cannot be called
  2861. * concurrently with either itself or regcache_sync().
  2862. */
  2863. int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
  2864. int num_regs)
  2865. {
  2866. struct reg_sequence *p;
  2867. int ret;
  2868. bool bypass;
  2869. if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
  2870. num_regs))
  2871. return 0;
  2872. p = krealloc(map->patch,
  2873. sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
  2874. GFP_KERNEL);
  2875. if (p) {
  2876. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  2877. map->patch = p;
  2878. map->patch_regs += num_regs;
  2879. } else {
  2880. return -ENOMEM;
  2881. }
  2882. map->lock(map->lock_arg);
  2883. bypass = map->cache_bypass;
  2884. map->cache_bypass = true;
  2885. map->async = true;
  2886. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2887. map->async = false;
  2888. map->cache_bypass = bypass;
  2889. map->unlock(map->lock_arg);
  2890. regmap_async_complete(map);
  2891. return ret;
  2892. }
  2893. EXPORT_SYMBOL_GPL(regmap_register_patch);
  2894. /**
  2895. * regmap_get_val_bytes() - Report the size of a register value
  2896. *
  2897. * @map: Register map to operate on.
  2898. *
  2899. * Report the size of a register value, mainly intended to for use by
  2900. * generic infrastructure built on top of regmap.
  2901. */
  2902. int regmap_get_val_bytes(struct regmap *map)
  2903. {
  2904. if (map->format.format_write)
  2905. return -EINVAL;
  2906. return map->format.val_bytes;
  2907. }
  2908. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  2909. /**
  2910. * regmap_get_max_register() - Report the max register value
  2911. *
  2912. * @map: Register map to operate on.
  2913. *
  2914. * Report the max register value, mainly intended to for use by
  2915. * generic infrastructure built on top of regmap.
  2916. */
  2917. int regmap_get_max_register(struct regmap *map)
  2918. {
  2919. return map->max_register_is_set ? map->max_register : -EINVAL;
  2920. }
  2921. EXPORT_SYMBOL_GPL(regmap_get_max_register);
  2922. /**
  2923. * regmap_get_reg_stride() - Report the register address stride
  2924. *
  2925. * @map: Register map to operate on.
  2926. *
  2927. * Report the register address stride, mainly intended to for use by
  2928. * generic infrastructure built on top of regmap.
  2929. */
  2930. int regmap_get_reg_stride(struct regmap *map)
  2931. {
  2932. return map->reg_stride;
  2933. }
  2934. EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
  2935. /**
  2936. * regmap_might_sleep() - Returns whether a regmap access might sleep.
  2937. *
  2938. * @map: Register map to operate on.
  2939. *
  2940. * Returns true if an access to the register might sleep, else false.
  2941. */
  2942. bool regmap_might_sleep(struct regmap *map)
  2943. {
  2944. return map->can_sleep;
  2945. }
  2946. EXPORT_SYMBOL_GPL(regmap_might_sleep);
  2947. int regmap_parse_val(struct regmap *map, const void *buf,
  2948. unsigned int *val)
  2949. {
  2950. if (!map->format.parse_val)
  2951. return -EINVAL;
  2952. *val = map->format.parse_val(buf);
  2953. return 0;
  2954. }
  2955. EXPORT_SYMBOL_GPL(regmap_parse_val);
  2956. static int __init regmap_initcall(void)
  2957. {
  2958. regmap_debugfs_initcall();
  2959. return 0;
  2960. }
  2961. postcore_initcall(regmap_initcall);