regmap.c 37 KB

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