regmap.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. #define CREATE_TRACE_POINTS
  19. #include <trace/events/regmap.h>
  20. #include "internal.h"
  21. /*
  22. * Sometimes for failures during very early init the trace
  23. * infrastructure isn't available early enough to be used. For this
  24. * sort of problem defining LOG_DEVICE will add printks for basic
  25. * register I/O on a specific device.
  26. */
  27. #undef LOG_DEVICE
  28. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  29. unsigned int mask, unsigned int val,
  30. bool *change);
  31. bool regmap_reg_in_ranges(unsigned int reg,
  32. const struct regmap_range *ranges,
  33. unsigned int nranges)
  34. {
  35. const struct regmap_range *r;
  36. int i;
  37. for (i = 0, r = ranges; i < nranges; i++, r++)
  38. if (regmap_reg_in_range(reg, r))
  39. return true;
  40. return false;
  41. }
  42. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  43. static bool _regmap_check_range_table(struct regmap *map,
  44. unsigned int reg,
  45. const struct regmap_access_table *table)
  46. {
  47. /* Check "no ranges" first */
  48. if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
  49. return false;
  50. /* In case zero "yes ranges" are supplied, any reg is OK */
  51. if (!table->n_yes_ranges)
  52. return true;
  53. return regmap_reg_in_ranges(reg, table->yes_ranges,
  54. table->n_yes_ranges);
  55. }
  56. bool regmap_writeable(struct regmap *map, unsigned int reg)
  57. {
  58. if (map->max_register && reg > map->max_register)
  59. return false;
  60. if (map->writeable_reg)
  61. return map->writeable_reg(map->dev, reg);
  62. if (map->wr_table)
  63. return _regmap_check_range_table(map, reg, map->wr_table);
  64. return true;
  65. }
  66. bool regmap_readable(struct regmap *map, unsigned int reg)
  67. {
  68. if (map->max_register && reg > map->max_register)
  69. return false;
  70. if (map->format.format_write)
  71. return false;
  72. if (map->readable_reg)
  73. return map->readable_reg(map->dev, reg);
  74. if (map->rd_table)
  75. return _regmap_check_range_table(map, reg, map->rd_table);
  76. return true;
  77. }
  78. bool regmap_volatile(struct regmap *map, unsigned int reg)
  79. {
  80. if (!regmap_readable(map, reg))
  81. return false;
  82. if (map->volatile_reg)
  83. return map->volatile_reg(map->dev, reg);
  84. if (map->volatile_table)
  85. return _regmap_check_range_table(map, reg, map->volatile_table);
  86. return true;
  87. }
  88. bool regmap_precious(struct regmap *map, unsigned int reg)
  89. {
  90. if (!regmap_readable(map, reg))
  91. return false;
  92. if (map->precious_reg)
  93. return map->precious_reg(map->dev, reg);
  94. if (map->precious_table)
  95. return _regmap_check_range_table(map, reg, map->precious_table);
  96. return false;
  97. }
  98. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  99. size_t num)
  100. {
  101. unsigned int i;
  102. for (i = 0; i < num; i++)
  103. if (!regmap_volatile(map, reg + i))
  104. return false;
  105. return true;
  106. }
  107. static void regmap_format_2_6_write(struct regmap *map,
  108. unsigned int reg, unsigned int val)
  109. {
  110. u8 *out = map->work_buf;
  111. *out = (reg << 6) | val;
  112. }
  113. static void regmap_format_4_12_write(struct regmap *map,
  114. unsigned int reg, unsigned int val)
  115. {
  116. __be16 *out = map->work_buf;
  117. *out = cpu_to_be16((reg << 12) | val);
  118. }
  119. static void regmap_format_7_9_write(struct regmap *map,
  120. unsigned int reg, unsigned int val)
  121. {
  122. __be16 *out = map->work_buf;
  123. *out = cpu_to_be16((reg << 9) | val);
  124. }
  125. static void regmap_format_10_14_write(struct regmap *map,
  126. unsigned int reg, unsigned int val)
  127. {
  128. u8 *out = map->work_buf;
  129. out[2] = val;
  130. out[1] = (val >> 8) | (reg << 6);
  131. out[0] = reg >> 2;
  132. }
  133. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  134. {
  135. u8 *b = buf;
  136. b[0] = val << shift;
  137. }
  138. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  139. {
  140. __be16 *b = buf;
  141. b[0] = cpu_to_be16(val << shift);
  142. }
  143. static void regmap_format_16_native(void *buf, unsigned int val,
  144. unsigned int shift)
  145. {
  146. *(u16 *)buf = val << shift;
  147. }
  148. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  149. {
  150. u8 *b = buf;
  151. val <<= shift;
  152. b[0] = val >> 16;
  153. b[1] = val >> 8;
  154. b[2] = val;
  155. }
  156. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  157. {
  158. __be32 *b = buf;
  159. b[0] = cpu_to_be32(val << shift);
  160. }
  161. static void regmap_format_32_native(void *buf, unsigned int val,
  162. unsigned int shift)
  163. {
  164. *(u32 *)buf = val << shift;
  165. }
  166. static unsigned int regmap_parse_8(void *buf)
  167. {
  168. u8 *b = buf;
  169. return b[0];
  170. }
  171. static unsigned int regmap_parse_16_be(void *buf)
  172. {
  173. __be16 *b = buf;
  174. b[0] = be16_to_cpu(b[0]);
  175. return b[0];
  176. }
  177. static unsigned int regmap_parse_16_native(void *buf)
  178. {
  179. return *(u16 *)buf;
  180. }
  181. static unsigned int regmap_parse_24(void *buf)
  182. {
  183. u8 *b = buf;
  184. unsigned int ret = b[2];
  185. ret |= ((unsigned int)b[1]) << 8;
  186. ret |= ((unsigned int)b[0]) << 16;
  187. return ret;
  188. }
  189. static unsigned int regmap_parse_32_be(void *buf)
  190. {
  191. __be32 *b = buf;
  192. b[0] = be32_to_cpu(b[0]);
  193. return b[0];
  194. }
  195. static unsigned int regmap_parse_32_native(void *buf)
  196. {
  197. return *(u32 *)buf;
  198. }
  199. static void regmap_lock_mutex(void *__map)
  200. {
  201. struct regmap *map = __map;
  202. mutex_lock(&map->mutex);
  203. }
  204. static void regmap_unlock_mutex(void *__map)
  205. {
  206. struct regmap *map = __map;
  207. mutex_unlock(&map->mutex);
  208. }
  209. static void regmap_lock_spinlock(void *__map)
  210. {
  211. struct regmap *map = __map;
  212. spin_lock(&map->spinlock);
  213. }
  214. static void regmap_unlock_spinlock(void *__map)
  215. {
  216. struct regmap *map = __map;
  217. spin_unlock(&map->spinlock);
  218. }
  219. static void dev_get_regmap_release(struct device *dev, void *res)
  220. {
  221. /*
  222. * We don't actually have anything to do here; the goal here
  223. * is not to manage the regmap but to provide a simple way to
  224. * get the regmap back given a struct device.
  225. */
  226. }
  227. static bool _regmap_range_add(struct regmap *map,
  228. struct regmap_range_node *data)
  229. {
  230. struct rb_root *root = &map->range_tree;
  231. struct rb_node **new = &(root->rb_node), *parent = NULL;
  232. while (*new) {
  233. struct regmap_range_node *this =
  234. container_of(*new, struct regmap_range_node, node);
  235. parent = *new;
  236. if (data->range_max < this->range_min)
  237. new = &((*new)->rb_left);
  238. else if (data->range_min > this->range_max)
  239. new = &((*new)->rb_right);
  240. else
  241. return false;
  242. }
  243. rb_link_node(&data->node, parent, new);
  244. rb_insert_color(&data->node, root);
  245. return true;
  246. }
  247. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  248. unsigned int reg)
  249. {
  250. struct rb_node *node = map->range_tree.rb_node;
  251. while (node) {
  252. struct regmap_range_node *this =
  253. container_of(node, struct regmap_range_node, node);
  254. if (reg < this->range_min)
  255. node = node->rb_left;
  256. else if (reg > this->range_max)
  257. node = node->rb_right;
  258. else
  259. return this;
  260. }
  261. return NULL;
  262. }
  263. static void regmap_range_exit(struct regmap *map)
  264. {
  265. struct rb_node *next;
  266. struct regmap_range_node *range_node;
  267. next = rb_first(&map->range_tree);
  268. while (next) {
  269. range_node = rb_entry(next, struct regmap_range_node, node);
  270. next = rb_next(&range_node->node);
  271. rb_erase(&range_node->node, &map->range_tree);
  272. kfree(range_node);
  273. }
  274. kfree(map->selector_work_buf);
  275. }
  276. /**
  277. * regmap_init(): Initialise register map
  278. *
  279. * @dev: Device that will be interacted with
  280. * @bus: Bus-specific callbacks to use with device
  281. * @bus_context: Data passed to bus-specific callbacks
  282. * @config: Configuration for register map
  283. *
  284. * The return value will be an ERR_PTR() on error or a valid pointer to
  285. * a struct regmap. This function should generally not be called
  286. * directly, it should be called by bus-specific init functions.
  287. */
  288. struct regmap *regmap_init(struct device *dev,
  289. const struct regmap_bus *bus,
  290. void *bus_context,
  291. const struct regmap_config *config)
  292. {
  293. struct regmap *map, **m;
  294. int ret = -EINVAL;
  295. enum regmap_endian reg_endian, val_endian;
  296. int i, j;
  297. if (!bus || !config)
  298. goto err;
  299. map = kzalloc(sizeof(*map), GFP_KERNEL);
  300. if (map == NULL) {
  301. ret = -ENOMEM;
  302. goto err;
  303. }
  304. if (config->lock && config->unlock) {
  305. map->lock = config->lock;
  306. map->unlock = config->unlock;
  307. map->lock_arg = config->lock_arg;
  308. } else {
  309. if (bus->fast_io) {
  310. spin_lock_init(&map->spinlock);
  311. map->lock = regmap_lock_spinlock;
  312. map->unlock = regmap_unlock_spinlock;
  313. } else {
  314. mutex_init(&map->mutex);
  315. map->lock = regmap_lock_mutex;
  316. map->unlock = regmap_unlock_mutex;
  317. }
  318. map->lock_arg = map;
  319. }
  320. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  321. map->format.pad_bytes = config->pad_bits / 8;
  322. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  323. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  324. config->val_bits + config->pad_bits, 8);
  325. map->reg_shift = config->pad_bits % 8;
  326. if (config->reg_stride)
  327. map->reg_stride = config->reg_stride;
  328. else
  329. map->reg_stride = 1;
  330. map->use_single_rw = config->use_single_rw;
  331. map->dev = dev;
  332. map->bus = bus;
  333. map->bus_context = bus_context;
  334. map->max_register = config->max_register;
  335. map->wr_table = config->wr_table;
  336. map->rd_table = config->rd_table;
  337. map->volatile_table = config->volatile_table;
  338. map->precious_table = config->precious_table;
  339. map->writeable_reg = config->writeable_reg;
  340. map->readable_reg = config->readable_reg;
  341. map->volatile_reg = config->volatile_reg;
  342. map->precious_reg = config->precious_reg;
  343. map->cache_type = config->cache_type;
  344. map->name = config->name;
  345. if (config->read_flag_mask || config->write_flag_mask) {
  346. map->read_flag_mask = config->read_flag_mask;
  347. map->write_flag_mask = config->write_flag_mask;
  348. } else {
  349. map->read_flag_mask = bus->read_flag_mask;
  350. }
  351. reg_endian = config->reg_format_endian;
  352. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  353. reg_endian = bus->reg_format_endian_default;
  354. if (reg_endian == REGMAP_ENDIAN_DEFAULT)
  355. reg_endian = REGMAP_ENDIAN_BIG;
  356. val_endian = config->val_format_endian;
  357. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  358. val_endian = bus->val_format_endian_default;
  359. if (val_endian == REGMAP_ENDIAN_DEFAULT)
  360. val_endian = REGMAP_ENDIAN_BIG;
  361. switch (config->reg_bits + map->reg_shift) {
  362. case 2:
  363. switch (config->val_bits) {
  364. case 6:
  365. map->format.format_write = regmap_format_2_6_write;
  366. break;
  367. default:
  368. goto err_map;
  369. }
  370. break;
  371. case 4:
  372. switch (config->val_bits) {
  373. case 12:
  374. map->format.format_write = regmap_format_4_12_write;
  375. break;
  376. default:
  377. goto err_map;
  378. }
  379. break;
  380. case 7:
  381. switch (config->val_bits) {
  382. case 9:
  383. map->format.format_write = regmap_format_7_9_write;
  384. break;
  385. default:
  386. goto err_map;
  387. }
  388. break;
  389. case 10:
  390. switch (config->val_bits) {
  391. case 14:
  392. map->format.format_write = regmap_format_10_14_write;
  393. break;
  394. default:
  395. goto err_map;
  396. }
  397. break;
  398. case 8:
  399. map->format.format_reg = regmap_format_8;
  400. break;
  401. case 16:
  402. switch (reg_endian) {
  403. case REGMAP_ENDIAN_BIG:
  404. map->format.format_reg = regmap_format_16_be;
  405. break;
  406. case REGMAP_ENDIAN_NATIVE:
  407. map->format.format_reg = regmap_format_16_native;
  408. break;
  409. default:
  410. goto err_map;
  411. }
  412. break;
  413. case 32:
  414. switch (reg_endian) {
  415. case REGMAP_ENDIAN_BIG:
  416. map->format.format_reg = regmap_format_32_be;
  417. break;
  418. case REGMAP_ENDIAN_NATIVE:
  419. map->format.format_reg = regmap_format_32_native;
  420. break;
  421. default:
  422. goto err_map;
  423. }
  424. break;
  425. default:
  426. goto err_map;
  427. }
  428. switch (config->val_bits) {
  429. case 8:
  430. map->format.format_val = regmap_format_8;
  431. map->format.parse_val = regmap_parse_8;
  432. break;
  433. case 16:
  434. switch (val_endian) {
  435. case REGMAP_ENDIAN_BIG:
  436. map->format.format_val = regmap_format_16_be;
  437. map->format.parse_val = regmap_parse_16_be;
  438. break;
  439. case REGMAP_ENDIAN_NATIVE:
  440. map->format.format_val = regmap_format_16_native;
  441. map->format.parse_val = regmap_parse_16_native;
  442. break;
  443. default:
  444. goto err_map;
  445. }
  446. break;
  447. case 24:
  448. if (val_endian != REGMAP_ENDIAN_BIG)
  449. goto err_map;
  450. map->format.format_val = regmap_format_24;
  451. map->format.parse_val = regmap_parse_24;
  452. break;
  453. case 32:
  454. switch (val_endian) {
  455. case REGMAP_ENDIAN_BIG:
  456. map->format.format_val = regmap_format_32_be;
  457. map->format.parse_val = regmap_parse_32_be;
  458. break;
  459. case REGMAP_ENDIAN_NATIVE:
  460. map->format.format_val = regmap_format_32_native;
  461. map->format.parse_val = regmap_parse_32_native;
  462. break;
  463. default:
  464. goto err_map;
  465. }
  466. break;
  467. }
  468. if (map->format.format_write) {
  469. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  470. (val_endian != REGMAP_ENDIAN_BIG))
  471. goto err_map;
  472. map->use_single_rw = true;
  473. }
  474. if (!map->format.format_write &&
  475. !(map->format.format_reg && map->format.format_val))
  476. goto err_map;
  477. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  478. if (map->work_buf == NULL) {
  479. ret = -ENOMEM;
  480. goto err_map;
  481. }
  482. map->range_tree = RB_ROOT;
  483. for (i = 0; i < config->num_ranges; i++) {
  484. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  485. struct regmap_range_node *new;
  486. /* Sanity check */
  487. if (range_cfg->range_max < range_cfg->range_min) {
  488. dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
  489. range_cfg->range_max, range_cfg->range_min);
  490. goto err_range;
  491. }
  492. if (range_cfg->range_max > map->max_register) {
  493. dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
  494. range_cfg->range_max, map->max_register);
  495. goto err_range;
  496. }
  497. if (range_cfg->selector_reg > map->max_register) {
  498. dev_err(map->dev,
  499. "Invalid range %d: selector out of map\n", i);
  500. goto err_range;
  501. }
  502. if (range_cfg->window_len == 0) {
  503. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  504. i);
  505. goto err_range;
  506. }
  507. /* Make sure, that this register range has no selector
  508. or data window within its boundary */
  509. for (j = 0; j < config->num_ranges; j++) {
  510. unsigned sel_reg = config->ranges[j].selector_reg;
  511. unsigned win_min = config->ranges[j].window_start;
  512. unsigned win_max = win_min +
  513. config->ranges[j].window_len - 1;
  514. if (range_cfg->range_min <= sel_reg &&
  515. sel_reg <= range_cfg->range_max) {
  516. dev_err(map->dev,
  517. "Range %d: selector for %d in window\n",
  518. i, j);
  519. goto err_range;
  520. }
  521. if (!(win_max < range_cfg->range_min ||
  522. win_min > range_cfg->range_max)) {
  523. dev_err(map->dev,
  524. "Range %d: window for %d in window\n",
  525. i, j);
  526. goto err_range;
  527. }
  528. }
  529. new = kzalloc(sizeof(*new), GFP_KERNEL);
  530. if (new == NULL) {
  531. ret = -ENOMEM;
  532. goto err_range;
  533. }
  534. new->map = map;
  535. new->name = range_cfg->name;
  536. new->range_min = range_cfg->range_min;
  537. new->range_max = range_cfg->range_max;
  538. new->selector_reg = range_cfg->selector_reg;
  539. new->selector_mask = range_cfg->selector_mask;
  540. new->selector_shift = range_cfg->selector_shift;
  541. new->window_start = range_cfg->window_start;
  542. new->window_len = range_cfg->window_len;
  543. if (_regmap_range_add(map, new) == false) {
  544. dev_err(map->dev, "Failed to add range %d\n", i);
  545. kfree(new);
  546. goto err_range;
  547. }
  548. if (map->selector_work_buf == NULL) {
  549. map->selector_work_buf =
  550. kzalloc(map->format.buf_size, GFP_KERNEL);
  551. if (map->selector_work_buf == NULL) {
  552. ret = -ENOMEM;
  553. goto err_range;
  554. }
  555. }
  556. }
  557. ret = regcache_init(map, config);
  558. if (ret != 0)
  559. goto err_range;
  560. regmap_debugfs_init(map, config->name);
  561. /* Add a devres resource for dev_get_regmap() */
  562. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  563. if (!m) {
  564. ret = -ENOMEM;
  565. goto err_debugfs;
  566. }
  567. *m = map;
  568. devres_add(dev, m);
  569. return map;
  570. err_debugfs:
  571. regmap_debugfs_exit(map);
  572. regcache_exit(map);
  573. err_range:
  574. regmap_range_exit(map);
  575. kfree(map->work_buf);
  576. err_map:
  577. kfree(map);
  578. err:
  579. return ERR_PTR(ret);
  580. }
  581. EXPORT_SYMBOL_GPL(regmap_init);
  582. static void devm_regmap_release(struct device *dev, void *res)
  583. {
  584. regmap_exit(*(struct regmap **)res);
  585. }
  586. /**
  587. * devm_regmap_init(): Initialise managed register map
  588. *
  589. * @dev: Device that will be interacted with
  590. * @bus: Bus-specific callbacks to use with device
  591. * @bus_context: Data passed to bus-specific callbacks
  592. * @config: Configuration for register map
  593. *
  594. * The return value will be an ERR_PTR() on error or a valid pointer
  595. * to a struct regmap. This function should generally not be called
  596. * directly, it should be called by bus-specific init functions. The
  597. * map will be automatically freed by the device management code.
  598. */
  599. struct regmap *devm_regmap_init(struct device *dev,
  600. const struct regmap_bus *bus,
  601. void *bus_context,
  602. const struct regmap_config *config)
  603. {
  604. struct regmap **ptr, *regmap;
  605. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  606. if (!ptr)
  607. return ERR_PTR(-ENOMEM);
  608. regmap = regmap_init(dev, bus, bus_context, config);
  609. if (!IS_ERR(regmap)) {
  610. *ptr = regmap;
  611. devres_add(dev, ptr);
  612. } else {
  613. devres_free(ptr);
  614. }
  615. return regmap;
  616. }
  617. EXPORT_SYMBOL_GPL(devm_regmap_init);
  618. /**
  619. * regmap_reinit_cache(): Reinitialise the current register cache
  620. *
  621. * @map: Register map to operate on.
  622. * @config: New configuration. Only the cache data will be used.
  623. *
  624. * Discard any existing register cache for the map and initialize a
  625. * new cache. This can be used to restore the cache to defaults or to
  626. * update the cache configuration to reflect runtime discovery of the
  627. * hardware.
  628. *
  629. * No explicit locking is done here, the user needs to ensure that
  630. * this function will not race with other calls to regmap.
  631. */
  632. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  633. {
  634. regcache_exit(map);
  635. regmap_debugfs_exit(map);
  636. map->max_register = config->max_register;
  637. map->writeable_reg = config->writeable_reg;
  638. map->readable_reg = config->readable_reg;
  639. map->volatile_reg = config->volatile_reg;
  640. map->precious_reg = config->precious_reg;
  641. map->cache_type = config->cache_type;
  642. regmap_debugfs_init(map, config->name);
  643. map->cache_bypass = false;
  644. map->cache_only = false;
  645. return regcache_init(map, config);
  646. }
  647. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  648. /**
  649. * regmap_exit(): Free a previously allocated register map
  650. */
  651. void regmap_exit(struct regmap *map)
  652. {
  653. regcache_exit(map);
  654. regmap_debugfs_exit(map);
  655. regmap_range_exit(map);
  656. if (map->bus->free_context)
  657. map->bus->free_context(map->bus_context);
  658. kfree(map->work_buf);
  659. kfree(map);
  660. }
  661. EXPORT_SYMBOL_GPL(regmap_exit);
  662. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  663. {
  664. struct regmap **r = res;
  665. if (!r || !*r) {
  666. WARN_ON(!r || !*r);
  667. return 0;
  668. }
  669. /* If the user didn't specify a name match any */
  670. if (data)
  671. return (*r)->name == data;
  672. else
  673. return 1;
  674. }
  675. /**
  676. * dev_get_regmap(): Obtain the regmap (if any) for a device
  677. *
  678. * @dev: Device to retrieve the map for
  679. * @name: Optional name for the register map, usually NULL.
  680. *
  681. * Returns the regmap for the device if one is present, or NULL. If
  682. * name is specified then it must match the name specified when
  683. * registering the device, if it is NULL then the first regmap found
  684. * will be used. Devices with multiple register maps are very rare,
  685. * generic code should normally not need to specify a name.
  686. */
  687. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  688. {
  689. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  690. dev_get_regmap_match, (void *)name);
  691. if (!r)
  692. return NULL;
  693. return *r;
  694. }
  695. EXPORT_SYMBOL_GPL(dev_get_regmap);
  696. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  697. struct regmap_range_node *range,
  698. unsigned int val_num)
  699. {
  700. void *orig_work_buf;
  701. unsigned int win_offset;
  702. unsigned int win_page;
  703. bool page_chg;
  704. int ret;
  705. win_offset = (*reg - range->range_min) % range->window_len;
  706. win_page = (*reg - range->range_min) / range->window_len;
  707. if (val_num > 1) {
  708. /* Bulk write shouldn't cross range boundary */
  709. if (*reg + val_num - 1 > range->range_max)
  710. return -EINVAL;
  711. /* ... or single page boundary */
  712. if (val_num > range->window_len - win_offset)
  713. return -EINVAL;
  714. }
  715. /* It is possible to have selector register inside data window.
  716. In that case, selector register is located on every page and
  717. it needs no page switching, when accessed alone. */
  718. if (val_num > 1 ||
  719. range->window_start + win_offset != range->selector_reg) {
  720. /* Use separate work_buf during page switching */
  721. orig_work_buf = map->work_buf;
  722. map->work_buf = map->selector_work_buf;
  723. ret = _regmap_update_bits(map, range->selector_reg,
  724. range->selector_mask,
  725. win_page << range->selector_shift,
  726. &page_chg);
  727. map->work_buf = orig_work_buf;
  728. if (ret != 0)
  729. return ret;
  730. }
  731. *reg = range->window_start + win_offset;
  732. return 0;
  733. }
  734. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  735. const void *val, size_t val_len)
  736. {
  737. struct regmap_range_node *range;
  738. u8 *u8 = map->work_buf;
  739. void *buf;
  740. int ret = -ENOTSUPP;
  741. size_t len;
  742. int i;
  743. /* Check for unwritable registers before we start */
  744. if (map->writeable_reg)
  745. for (i = 0; i < val_len / map->format.val_bytes; i++)
  746. if (!map->writeable_reg(map->dev,
  747. reg + (i * map->reg_stride)))
  748. return -EINVAL;
  749. if (!map->cache_bypass && map->format.parse_val) {
  750. unsigned int ival;
  751. int val_bytes = map->format.val_bytes;
  752. for (i = 0; i < val_len / val_bytes; i++) {
  753. memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
  754. ival = map->format.parse_val(map->work_buf);
  755. ret = regcache_write(map, reg + (i * map->reg_stride),
  756. ival);
  757. if (ret) {
  758. dev_err(map->dev,
  759. "Error in caching of register: %x ret: %d\n",
  760. reg + i, ret);
  761. return ret;
  762. }
  763. }
  764. if (map->cache_only) {
  765. map->cache_dirty = true;
  766. return 0;
  767. }
  768. }
  769. range = _regmap_range_lookup(map, reg);
  770. if (range) {
  771. int val_num = val_len / map->format.val_bytes;
  772. int win_offset = (reg - range->range_min) % range->window_len;
  773. int win_residue = range->window_len - win_offset;
  774. /* If the write goes beyond the end of the window split it */
  775. while (val_num > win_residue) {
  776. dev_dbg(map->dev, "Writing window %d/%zu\n",
  777. win_residue, val_len / map->format.val_bytes);
  778. ret = _regmap_raw_write(map, reg, val, win_residue *
  779. map->format.val_bytes);
  780. if (ret != 0)
  781. return ret;
  782. reg += win_residue;
  783. val_num -= win_residue;
  784. val += win_residue * map->format.val_bytes;
  785. val_len -= win_residue * map->format.val_bytes;
  786. win_offset = (reg - range->range_min) %
  787. range->window_len;
  788. win_residue = range->window_len - win_offset;
  789. }
  790. ret = _regmap_select_page(map, &reg, range, val_num);
  791. if (ret != 0)
  792. return ret;
  793. }
  794. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  795. u8[0] |= map->write_flag_mask;
  796. trace_regmap_hw_write_start(map->dev, reg,
  797. val_len / map->format.val_bytes);
  798. /* If we're doing a single register write we can probably just
  799. * send the work_buf directly, otherwise try to do a gather
  800. * write.
  801. */
  802. if (val == (map->work_buf + map->format.pad_bytes +
  803. map->format.reg_bytes))
  804. ret = map->bus->write(map->bus_context, map->work_buf,
  805. map->format.reg_bytes +
  806. map->format.pad_bytes +
  807. val_len);
  808. else if (map->bus->gather_write)
  809. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  810. map->format.reg_bytes +
  811. map->format.pad_bytes,
  812. val, val_len);
  813. /* If that didn't work fall back on linearising by hand. */
  814. if (ret == -ENOTSUPP) {
  815. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  816. buf = kzalloc(len, GFP_KERNEL);
  817. if (!buf)
  818. return -ENOMEM;
  819. memcpy(buf, map->work_buf, map->format.reg_bytes);
  820. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  821. val, val_len);
  822. ret = map->bus->write(map->bus_context, buf, len);
  823. kfree(buf);
  824. }
  825. trace_regmap_hw_write_done(map->dev, reg,
  826. val_len / map->format.val_bytes);
  827. return ret;
  828. }
  829. int _regmap_write(struct regmap *map, unsigned int reg,
  830. unsigned int val)
  831. {
  832. struct regmap_range_node *range;
  833. int ret;
  834. BUG_ON(!map->format.format_write && !map->format.format_val);
  835. if (!map->cache_bypass && map->format.format_write) {
  836. ret = regcache_write(map, reg, val);
  837. if (ret != 0)
  838. return ret;
  839. if (map->cache_only) {
  840. map->cache_dirty = true;
  841. return 0;
  842. }
  843. }
  844. #ifdef LOG_DEVICE
  845. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  846. dev_info(map->dev, "%x <= %x\n", reg, val);
  847. #endif
  848. trace_regmap_reg_write(map->dev, reg, val);
  849. if (map->format.format_write) {
  850. range = _regmap_range_lookup(map, reg);
  851. if (range) {
  852. ret = _regmap_select_page(map, &reg, range, 1);
  853. if (ret != 0)
  854. return ret;
  855. }
  856. map->format.format_write(map, reg, val);
  857. trace_regmap_hw_write_start(map->dev, reg, 1);
  858. ret = map->bus->write(map->bus_context, map->work_buf,
  859. map->format.buf_size);
  860. trace_regmap_hw_write_done(map->dev, reg, 1);
  861. return ret;
  862. } else {
  863. map->format.format_val(map->work_buf + map->format.reg_bytes
  864. + map->format.pad_bytes, val, 0);
  865. return _regmap_raw_write(map, reg,
  866. map->work_buf +
  867. map->format.reg_bytes +
  868. map->format.pad_bytes,
  869. map->format.val_bytes);
  870. }
  871. }
  872. /**
  873. * regmap_write(): Write a value to a single register
  874. *
  875. * @map: Register map to write to
  876. * @reg: Register to write to
  877. * @val: Value to be written
  878. *
  879. * A value of zero will be returned on success, a negative errno will
  880. * be returned in error cases.
  881. */
  882. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  883. {
  884. int ret;
  885. if (reg % map->reg_stride)
  886. return -EINVAL;
  887. map->lock(map->lock_arg);
  888. ret = _regmap_write(map, reg, val);
  889. map->unlock(map->lock_arg);
  890. return ret;
  891. }
  892. EXPORT_SYMBOL_GPL(regmap_write);
  893. /**
  894. * regmap_raw_write(): Write raw values to one or more registers
  895. *
  896. * @map: Register map to write to
  897. * @reg: Initial register to write to
  898. * @val: Block of data to be written, laid out for direct transmission to the
  899. * device
  900. * @val_len: Length of data pointed to by val.
  901. *
  902. * This function is intended to be used for things like firmware
  903. * download where a large block of data needs to be transferred to the
  904. * device. No formatting will be done on the data provided.
  905. *
  906. * A value of zero will be returned on success, a negative errno will
  907. * be returned in error cases.
  908. */
  909. int regmap_raw_write(struct regmap *map, unsigned int reg,
  910. const void *val, size_t val_len)
  911. {
  912. int ret;
  913. if (val_len % map->format.val_bytes)
  914. return -EINVAL;
  915. if (reg % map->reg_stride)
  916. return -EINVAL;
  917. map->lock(map->lock_arg);
  918. ret = _regmap_raw_write(map, reg, val, val_len);
  919. map->unlock(map->lock_arg);
  920. return ret;
  921. }
  922. EXPORT_SYMBOL_GPL(regmap_raw_write);
  923. /*
  924. * regmap_bulk_write(): Write multiple registers to the device
  925. *
  926. * @map: Register map to write to
  927. * @reg: First register to be write from
  928. * @val: Block of data to be written, in native register size for device
  929. * @val_count: Number of registers to write
  930. *
  931. * This function is intended to be used for writing a large block of
  932. * data to be device either in single transfer or multiple transfer.
  933. *
  934. * A value of zero will be returned on success, a negative errno will
  935. * be returned in error cases.
  936. */
  937. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  938. size_t val_count)
  939. {
  940. int ret = 0, i;
  941. size_t val_bytes = map->format.val_bytes;
  942. void *wval;
  943. if (!map->format.parse_val)
  944. return -EINVAL;
  945. if (reg % map->reg_stride)
  946. return -EINVAL;
  947. map->lock(map->lock_arg);
  948. /* No formatting is require if val_byte is 1 */
  949. if (val_bytes == 1) {
  950. wval = (void *)val;
  951. } else {
  952. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  953. if (!wval) {
  954. ret = -ENOMEM;
  955. dev_err(map->dev, "Error in memory allocation\n");
  956. goto out;
  957. }
  958. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  959. map->format.parse_val(wval + i);
  960. }
  961. /*
  962. * Some devices does not support bulk write, for
  963. * them we have a series of single write operations.
  964. */
  965. if (map->use_single_rw) {
  966. for (i = 0; i < val_count; i++) {
  967. ret = regmap_raw_write(map,
  968. reg + (i * map->reg_stride),
  969. val + (i * val_bytes),
  970. val_bytes);
  971. if (ret != 0)
  972. return ret;
  973. }
  974. } else {
  975. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  976. }
  977. if (val_bytes != 1)
  978. kfree(wval);
  979. out:
  980. map->unlock(map->lock_arg);
  981. return ret;
  982. }
  983. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  984. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  985. unsigned int val_len)
  986. {
  987. struct regmap_range_node *range;
  988. u8 *u8 = map->work_buf;
  989. int ret;
  990. range = _regmap_range_lookup(map, reg);
  991. if (range) {
  992. ret = _regmap_select_page(map, &reg, range,
  993. val_len / map->format.val_bytes);
  994. if (ret != 0)
  995. return ret;
  996. }
  997. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  998. /*
  999. * Some buses or devices flag reads by setting the high bits in the
  1000. * register addresss; since it's always the high bits for all
  1001. * current formats we can do this here rather than in
  1002. * formatting. This may break if we get interesting formats.
  1003. */
  1004. u8[0] |= map->read_flag_mask;
  1005. trace_regmap_hw_read_start(map->dev, reg,
  1006. val_len / map->format.val_bytes);
  1007. ret = map->bus->read(map->bus_context, map->work_buf,
  1008. map->format.reg_bytes + map->format.pad_bytes,
  1009. val, val_len);
  1010. trace_regmap_hw_read_done(map->dev, reg,
  1011. val_len / map->format.val_bytes);
  1012. return ret;
  1013. }
  1014. static int _regmap_read(struct regmap *map, unsigned int reg,
  1015. unsigned int *val)
  1016. {
  1017. int ret;
  1018. if (!map->cache_bypass) {
  1019. ret = regcache_read(map, reg, val);
  1020. if (ret == 0)
  1021. return 0;
  1022. }
  1023. if (!map->format.parse_val)
  1024. return -EINVAL;
  1025. if (map->cache_only)
  1026. return -EBUSY;
  1027. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1028. if (ret == 0) {
  1029. *val = map->format.parse_val(map->work_buf);
  1030. #ifdef LOG_DEVICE
  1031. if (strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1032. dev_info(map->dev, "%x => %x\n", reg, *val);
  1033. #endif
  1034. trace_regmap_reg_read(map->dev, reg, *val);
  1035. }
  1036. if (ret == 0 && !map->cache_bypass)
  1037. regcache_write(map, reg, *val);
  1038. return ret;
  1039. }
  1040. /**
  1041. * regmap_read(): Read a value from a single register
  1042. *
  1043. * @map: Register map to write to
  1044. * @reg: Register to be read from
  1045. * @val: Pointer to store read value
  1046. *
  1047. * A value of zero will be returned on success, a negative errno will
  1048. * be returned in error cases.
  1049. */
  1050. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1051. {
  1052. int ret;
  1053. if (reg % map->reg_stride)
  1054. return -EINVAL;
  1055. map->lock(map->lock_arg);
  1056. ret = _regmap_read(map, reg, val);
  1057. map->unlock(map->lock_arg);
  1058. return ret;
  1059. }
  1060. EXPORT_SYMBOL_GPL(regmap_read);
  1061. /**
  1062. * regmap_raw_read(): Read raw data from the device
  1063. *
  1064. * @map: Register map to write to
  1065. * @reg: First register to be read from
  1066. * @val: Pointer to store read value
  1067. * @val_len: Size of data to read
  1068. *
  1069. * A value of zero will be returned on success, a negative errno will
  1070. * be returned in error cases.
  1071. */
  1072. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1073. size_t val_len)
  1074. {
  1075. size_t val_bytes = map->format.val_bytes;
  1076. size_t val_count = val_len / val_bytes;
  1077. unsigned int v;
  1078. int ret, i;
  1079. if (val_len % map->format.val_bytes)
  1080. return -EINVAL;
  1081. if (reg % map->reg_stride)
  1082. return -EINVAL;
  1083. map->lock(map->lock_arg);
  1084. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1085. map->cache_type == REGCACHE_NONE) {
  1086. /* Physical block read if there's no cache involved */
  1087. ret = _regmap_raw_read(map, reg, val, val_len);
  1088. } else {
  1089. /* Otherwise go word by word for the cache; should be low
  1090. * cost as we expect to hit the cache.
  1091. */
  1092. for (i = 0; i < val_count; i++) {
  1093. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1094. &v);
  1095. if (ret != 0)
  1096. goto out;
  1097. map->format.format_val(val + (i * val_bytes), v, 0);
  1098. }
  1099. }
  1100. out:
  1101. map->unlock(map->lock_arg);
  1102. return ret;
  1103. }
  1104. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1105. /**
  1106. * regmap_bulk_read(): Read multiple registers from the device
  1107. *
  1108. * @map: Register map to write to
  1109. * @reg: First register to be read from
  1110. * @val: Pointer to store read value, in native register size for device
  1111. * @val_count: Number of registers to read
  1112. *
  1113. * A value of zero will be returned on success, a negative errno will
  1114. * be returned in error cases.
  1115. */
  1116. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1117. size_t val_count)
  1118. {
  1119. int ret, i;
  1120. size_t val_bytes = map->format.val_bytes;
  1121. bool vol = regmap_volatile_range(map, reg, val_count);
  1122. if (!map->format.parse_val)
  1123. return -EINVAL;
  1124. if (reg % map->reg_stride)
  1125. return -EINVAL;
  1126. if (vol || map->cache_type == REGCACHE_NONE) {
  1127. /*
  1128. * Some devices does not support bulk read, for
  1129. * them we have a series of single read operations.
  1130. */
  1131. if (map->use_single_rw) {
  1132. for (i = 0; i < val_count; i++) {
  1133. ret = regmap_raw_read(map,
  1134. reg + (i * map->reg_stride),
  1135. val + (i * val_bytes),
  1136. val_bytes);
  1137. if (ret != 0)
  1138. return ret;
  1139. }
  1140. } else {
  1141. ret = regmap_raw_read(map, reg, val,
  1142. val_bytes * val_count);
  1143. if (ret != 0)
  1144. return ret;
  1145. }
  1146. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1147. map->format.parse_val(val + i);
  1148. } else {
  1149. for (i = 0; i < val_count; i++) {
  1150. unsigned int ival;
  1151. ret = regmap_read(map, reg + (i * map->reg_stride),
  1152. &ival);
  1153. if (ret != 0)
  1154. return ret;
  1155. memcpy(val + (i * val_bytes), &ival, val_bytes);
  1156. }
  1157. }
  1158. return 0;
  1159. }
  1160. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1161. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1162. unsigned int mask, unsigned int val,
  1163. bool *change)
  1164. {
  1165. int ret;
  1166. unsigned int tmp, orig;
  1167. ret = _regmap_read(map, reg, &orig);
  1168. if (ret != 0)
  1169. return ret;
  1170. tmp = orig & ~mask;
  1171. tmp |= val & mask;
  1172. if (tmp != orig) {
  1173. ret = _regmap_write(map, reg, tmp);
  1174. *change = true;
  1175. } else {
  1176. *change = false;
  1177. }
  1178. return ret;
  1179. }
  1180. /**
  1181. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1182. *
  1183. * @map: Register map to update
  1184. * @reg: Register to update
  1185. * @mask: Bitmask to change
  1186. * @val: New value for bitmask
  1187. *
  1188. * Returns zero for success, a negative number on error.
  1189. */
  1190. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1191. unsigned int mask, unsigned int val)
  1192. {
  1193. bool change;
  1194. int ret;
  1195. map->lock(map->lock_arg);
  1196. ret = _regmap_update_bits(map, reg, mask, val, &change);
  1197. map->unlock(map->lock_arg);
  1198. return ret;
  1199. }
  1200. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1201. /**
  1202. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  1203. * register map and report if updated
  1204. *
  1205. * @map: Register map to update
  1206. * @reg: Register to update
  1207. * @mask: Bitmask to change
  1208. * @val: New value for bitmask
  1209. * @change: Boolean indicating if a write was done
  1210. *
  1211. * Returns zero for success, a negative number on error.
  1212. */
  1213. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  1214. unsigned int mask, unsigned int val,
  1215. bool *change)
  1216. {
  1217. int ret;
  1218. map->lock(map->lock_arg);
  1219. ret = _regmap_update_bits(map, reg, mask, val, change);
  1220. map->unlock(map->lock_arg);
  1221. return ret;
  1222. }
  1223. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  1224. /**
  1225. * regmap_register_patch: Register and apply register updates to be applied
  1226. * on device initialistion
  1227. *
  1228. * @map: Register map to apply updates to.
  1229. * @regs: Values to update.
  1230. * @num_regs: Number of entries in regs.
  1231. *
  1232. * Register a set of register updates to be applied to the device
  1233. * whenever the device registers are synchronised with the cache and
  1234. * apply them immediately. Typically this is used to apply
  1235. * corrections to be applied to the device defaults on startup, such
  1236. * as the updates some vendors provide to undocumented registers.
  1237. */
  1238. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  1239. int num_regs)
  1240. {
  1241. int i, ret;
  1242. bool bypass;
  1243. /* If needed the implementation can be extended to support this */
  1244. if (map->patch)
  1245. return -EBUSY;
  1246. map->lock(map->lock_arg);
  1247. bypass = map->cache_bypass;
  1248. map->cache_bypass = true;
  1249. /* Write out first; it's useful to apply even if we fail later. */
  1250. for (i = 0; i < num_regs; i++) {
  1251. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1252. if (ret != 0) {
  1253. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  1254. regs[i].reg, regs[i].def, ret);
  1255. goto out;
  1256. }
  1257. }
  1258. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  1259. if (map->patch != NULL) {
  1260. memcpy(map->patch, regs,
  1261. num_regs * sizeof(struct reg_default));
  1262. map->patch_regs = num_regs;
  1263. } else {
  1264. ret = -ENOMEM;
  1265. }
  1266. out:
  1267. map->cache_bypass = bypass;
  1268. map->unlock(map->lock_arg);
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL_GPL(regmap_register_patch);
  1272. /*
  1273. * regmap_get_val_bytes(): Report the size of a register value
  1274. *
  1275. * Report the size of a register value, mainly intended to for use by
  1276. * generic infrastructure built on top of regmap.
  1277. */
  1278. int regmap_get_val_bytes(struct regmap *map)
  1279. {
  1280. if (map->format.format_write)
  1281. return -EINVAL;
  1282. return map->format.val_bytes;
  1283. }
  1284. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  1285. static int __init regmap_initcall(void)
  1286. {
  1287. regmap_debugfs_initcall();
  1288. return 0;
  1289. }
  1290. postcore_initcall(regmap_initcall);