regmap.c 34 KB

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