regmap.c 61 KB

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