regmap.c 71 KB

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