regmap.c 86 KB

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