regmap.c 54 KB

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