regmap.c 35 KB

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