regmap.c 68 KB

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