regmap.c 69 KB

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