regmap.c 74 KB

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