regmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/err.h>
  16. #define CREATE_TRACE_POINTS
  17. #include <trace/events/regmap.h>
  18. #include "internal.h"
  19. bool regmap_writeable(struct regmap *map, unsigned int reg)
  20. {
  21. if (map->max_register && reg > map->max_register)
  22. return false;
  23. if (map->writeable_reg)
  24. return map->writeable_reg(map->dev, reg);
  25. return true;
  26. }
  27. bool regmap_readable(struct regmap *map, unsigned int reg)
  28. {
  29. if (map->max_register && reg > map->max_register)
  30. return false;
  31. if (map->readable_reg)
  32. return map->readable_reg(map->dev, reg);
  33. return true;
  34. }
  35. bool regmap_volatile(struct regmap *map, unsigned int reg)
  36. {
  37. if (map->max_register && reg > map->max_register)
  38. return false;
  39. if (map->volatile_reg)
  40. return map->volatile_reg(map->dev, reg);
  41. return true;
  42. }
  43. bool regmap_precious(struct regmap *map, unsigned int reg)
  44. {
  45. if (map->max_register && reg > map->max_register)
  46. return false;
  47. if (map->precious_reg)
  48. return map->precious_reg(map->dev, reg);
  49. return false;
  50. }
  51. static void regmap_format_4_12_write(struct regmap *map,
  52. unsigned int reg, unsigned int val)
  53. {
  54. __be16 *out = map->work_buf;
  55. *out = cpu_to_be16((reg << 12) | val);
  56. }
  57. static void regmap_format_7_9_write(struct regmap *map,
  58. unsigned int reg, unsigned int val)
  59. {
  60. __be16 *out = map->work_buf;
  61. *out = cpu_to_be16((reg << 9) | val);
  62. }
  63. static void regmap_format_8(void *buf, unsigned int val)
  64. {
  65. u8 *b = buf;
  66. b[0] = val;
  67. }
  68. static void regmap_format_16(void *buf, unsigned int val)
  69. {
  70. __be16 *b = buf;
  71. b[0] = cpu_to_be16(val);
  72. }
  73. static unsigned int regmap_parse_8(void *buf)
  74. {
  75. u8 *b = buf;
  76. return b[0];
  77. }
  78. static unsigned int regmap_parse_16(void *buf)
  79. {
  80. __be16 *b = buf;
  81. b[0] = be16_to_cpu(b[0]);
  82. return b[0];
  83. }
  84. /**
  85. * regmap_init(): Initialise register map
  86. *
  87. * @dev: Device that will be interacted with
  88. * @bus: Bus-specific callbacks to use with device
  89. * @config: Configuration for register map
  90. *
  91. * The return value will be an ERR_PTR() on error or a valid pointer to
  92. * a struct regmap. This function should generally not be called
  93. * directly, it should be called by bus-specific init functions.
  94. */
  95. struct regmap *regmap_init(struct device *dev,
  96. const struct regmap_bus *bus,
  97. const struct regmap_config *config)
  98. {
  99. struct regmap *map;
  100. int ret = -EINVAL;
  101. if (!bus || !config)
  102. return NULL;
  103. map = kzalloc(sizeof(*map), GFP_KERNEL);
  104. if (map == NULL) {
  105. ret = -ENOMEM;
  106. goto err;
  107. }
  108. mutex_init(&map->lock);
  109. map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
  110. map->format.reg_bytes = config->reg_bits / 8;
  111. map->format.val_bytes = config->val_bits / 8;
  112. map->dev = dev;
  113. map->bus = bus;
  114. map->max_register = config->max_register;
  115. map->writeable_reg = config->writeable_reg;
  116. map->readable_reg = config->readable_reg;
  117. map->volatile_reg = config->volatile_reg;
  118. map->precious_reg = config->precious_reg;
  119. map->cache_type = config->cache_type;
  120. if (config->read_flag_mask || config->write_flag_mask) {
  121. map->read_flag_mask = config->read_flag_mask;
  122. map->write_flag_mask = config->write_flag_mask;
  123. } else {
  124. map->read_flag_mask = bus->read_flag_mask;
  125. }
  126. switch (config->reg_bits) {
  127. case 4:
  128. switch (config->val_bits) {
  129. case 12:
  130. map->format.format_write = regmap_format_4_12_write;
  131. break;
  132. default:
  133. goto err_map;
  134. }
  135. break;
  136. case 7:
  137. switch (config->val_bits) {
  138. case 9:
  139. map->format.format_write = regmap_format_7_9_write;
  140. break;
  141. default:
  142. goto err_map;
  143. }
  144. break;
  145. case 8:
  146. map->format.format_reg = regmap_format_8;
  147. break;
  148. case 16:
  149. map->format.format_reg = regmap_format_16;
  150. break;
  151. default:
  152. goto err_map;
  153. }
  154. switch (config->val_bits) {
  155. case 8:
  156. map->format.format_val = regmap_format_8;
  157. map->format.parse_val = regmap_parse_8;
  158. break;
  159. case 16:
  160. map->format.format_val = regmap_format_16;
  161. map->format.parse_val = regmap_parse_16;
  162. break;
  163. }
  164. if (!map->format.format_write &&
  165. !(map->format.format_reg && map->format.format_val))
  166. goto err_map;
  167. map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
  168. if (map->work_buf == NULL) {
  169. ret = -ENOMEM;
  170. goto err_map;
  171. }
  172. ret = regcache_init(map, config);
  173. if (ret < 0)
  174. goto err_map;
  175. regmap_debugfs_init(map);
  176. return map;
  177. err_map:
  178. kfree(map);
  179. err:
  180. return ERR_PTR(ret);
  181. }
  182. EXPORT_SYMBOL_GPL(regmap_init);
  183. /**
  184. * regmap_reinit_cache(): Reinitialise the current register cache
  185. *
  186. * @map: Register map to operate on.
  187. * @config: New configuration. Only the cache data will be used.
  188. *
  189. * Discard any existing register cache for the map and initialize a
  190. * new cache. This can be used to restore the cache to defaults or to
  191. * update the cache configuration to reflect runtime discovery of the
  192. * hardware.
  193. */
  194. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  195. {
  196. int ret;
  197. mutex_lock(&map->lock);
  198. regcache_exit(map);
  199. map->max_register = config->max_register;
  200. map->writeable_reg = config->writeable_reg;
  201. map->readable_reg = config->readable_reg;
  202. map->volatile_reg = config->volatile_reg;
  203. map->precious_reg = config->precious_reg;
  204. map->cache_type = config->cache_type;
  205. ret = regcache_init(map, config);
  206. mutex_unlock(&map->lock);
  207. return ret;
  208. }
  209. /**
  210. * regmap_exit(): Free a previously allocated register map
  211. */
  212. void regmap_exit(struct regmap *map)
  213. {
  214. regcache_exit(map);
  215. regmap_debugfs_exit(map);
  216. kfree(map->work_buf);
  217. kfree(map);
  218. }
  219. EXPORT_SYMBOL_GPL(regmap_exit);
  220. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  221. const void *val, size_t val_len)
  222. {
  223. u8 *u8 = map->work_buf;
  224. void *buf;
  225. int ret = -ENOTSUPP;
  226. size_t len;
  227. int i;
  228. /* Check for unwritable registers before we start */
  229. if (map->writeable_reg)
  230. for (i = 0; i < val_len / map->format.val_bytes; i++)
  231. if (!map->writeable_reg(map->dev, reg + i))
  232. return -EINVAL;
  233. map->format.format_reg(map->work_buf, reg);
  234. u8[0] |= map->write_flag_mask;
  235. trace_regmap_hw_write_start(map->dev, reg,
  236. val_len / map->format.val_bytes);
  237. /* If we're doing a single register write we can probably just
  238. * send the work_buf directly, otherwise try to do a gather
  239. * write.
  240. */
  241. if (val == map->work_buf + map->format.reg_bytes)
  242. ret = map->bus->write(map->dev, map->work_buf,
  243. map->format.reg_bytes + val_len);
  244. else if (map->bus->gather_write)
  245. ret = map->bus->gather_write(map->dev, map->work_buf,
  246. map->format.reg_bytes,
  247. val, val_len);
  248. /* If that didn't work fall back on linearising by hand. */
  249. if (ret == -ENOTSUPP) {
  250. len = map->format.reg_bytes + val_len;
  251. buf = kmalloc(len, GFP_KERNEL);
  252. if (!buf)
  253. return -ENOMEM;
  254. memcpy(buf, map->work_buf, map->format.reg_bytes);
  255. memcpy(buf + map->format.reg_bytes, val, val_len);
  256. ret = map->bus->write(map->dev, buf, len);
  257. kfree(buf);
  258. }
  259. trace_regmap_hw_write_done(map->dev, reg,
  260. val_len / map->format.val_bytes);
  261. return ret;
  262. }
  263. int _regmap_write(struct regmap *map, unsigned int reg,
  264. unsigned int val)
  265. {
  266. int ret;
  267. BUG_ON(!map->format.format_write && !map->format.format_val);
  268. if (!map->cache_bypass) {
  269. ret = regcache_write(map, reg, val);
  270. if (ret != 0)
  271. return ret;
  272. if (map->cache_only) {
  273. map->cache_dirty = true;
  274. return 0;
  275. }
  276. }
  277. trace_regmap_reg_write(map->dev, reg, val);
  278. if (map->format.format_write) {
  279. map->format.format_write(map, reg, val);
  280. trace_regmap_hw_write_start(map->dev, reg, 1);
  281. ret = map->bus->write(map->dev, map->work_buf,
  282. map->format.buf_size);
  283. trace_regmap_hw_write_done(map->dev, reg, 1);
  284. return ret;
  285. } else {
  286. map->format.format_val(map->work_buf + map->format.reg_bytes,
  287. val);
  288. return _regmap_raw_write(map, reg,
  289. map->work_buf + map->format.reg_bytes,
  290. map->format.val_bytes);
  291. }
  292. }
  293. /**
  294. * regmap_write(): Write a value to a single register
  295. *
  296. * @map: Register map to write to
  297. * @reg: Register to write to
  298. * @val: Value to be written
  299. *
  300. * A value of zero will be returned on success, a negative errno will
  301. * be returned in error cases.
  302. */
  303. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  304. {
  305. int ret;
  306. mutex_lock(&map->lock);
  307. ret = _regmap_write(map, reg, val);
  308. mutex_unlock(&map->lock);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL_GPL(regmap_write);
  312. /**
  313. * regmap_raw_write(): Write raw values to one or more registers
  314. *
  315. * @map: Register map to write to
  316. * @reg: Initial register to write to
  317. * @val: Block of data to be written, laid out for direct transmission to the
  318. * device
  319. * @val_len: Length of data pointed to by val.
  320. *
  321. * This function is intended to be used for things like firmware
  322. * download where a large block of data needs to be transferred to the
  323. * device. No formatting will be done on the data provided.
  324. *
  325. * A value of zero will be returned on success, a negative errno will
  326. * be returned in error cases.
  327. */
  328. int regmap_raw_write(struct regmap *map, unsigned int reg,
  329. const void *val, size_t val_len)
  330. {
  331. int ret;
  332. WARN_ON(map->cache_type != REGCACHE_NONE);
  333. mutex_lock(&map->lock);
  334. ret = _regmap_raw_write(map, reg, val, val_len);
  335. mutex_unlock(&map->lock);
  336. return ret;
  337. }
  338. EXPORT_SYMBOL_GPL(regmap_raw_write);
  339. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  340. unsigned int val_len)
  341. {
  342. u8 *u8 = map->work_buf;
  343. int ret;
  344. map->format.format_reg(map->work_buf, reg);
  345. /*
  346. * Some buses or devices flag reads by setting the high bits in the
  347. * register addresss; since it's always the high bits for all
  348. * current formats we can do this here rather than in
  349. * formatting. This may break if we get interesting formats.
  350. */
  351. u8[0] |= map->read_flag_mask;
  352. trace_regmap_hw_read_start(map->dev, reg,
  353. val_len / map->format.val_bytes);
  354. ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
  355. val, val_len);
  356. trace_regmap_hw_read_done(map->dev, reg,
  357. val_len / map->format.val_bytes);
  358. return ret;
  359. }
  360. static int _regmap_read(struct regmap *map, unsigned int reg,
  361. unsigned int *val)
  362. {
  363. int ret;
  364. if (!map->format.parse_val)
  365. return -EINVAL;
  366. if (!map->cache_bypass) {
  367. ret = regcache_read(map, reg, val);
  368. if (ret == 0)
  369. return 0;
  370. }
  371. if (map->cache_only)
  372. return -EBUSY;
  373. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  374. if (ret == 0) {
  375. *val = map->format.parse_val(map->work_buf);
  376. trace_regmap_reg_read(map->dev, reg, *val);
  377. }
  378. return ret;
  379. }
  380. /**
  381. * regmap_read(): Read a value from a single register
  382. *
  383. * @map: Register map to write to
  384. * @reg: Register to be read from
  385. * @val: Pointer to store read value
  386. *
  387. * A value of zero will be returned on success, a negative errno will
  388. * be returned in error cases.
  389. */
  390. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  391. {
  392. int ret;
  393. mutex_lock(&map->lock);
  394. ret = _regmap_read(map, reg, val);
  395. mutex_unlock(&map->lock);
  396. return ret;
  397. }
  398. EXPORT_SYMBOL_GPL(regmap_read);
  399. /**
  400. * regmap_raw_read(): Read raw data from the device
  401. *
  402. * @map: Register map to write to
  403. * @reg: First register to be read from
  404. * @val: Pointer to store read value
  405. * @val_len: Size of data to read
  406. *
  407. * A value of zero will be returned on success, a negative errno will
  408. * be returned in error cases.
  409. */
  410. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  411. size_t val_len)
  412. {
  413. int ret;
  414. int i;
  415. bool vol = true;
  416. for (i = 0; i < val_len / map->format.val_bytes; i++)
  417. if (!regmap_volatile(map, reg + i))
  418. vol = false;
  419. WARN_ON(!vol && map->cache_type != REGCACHE_NONE);
  420. mutex_lock(&map->lock);
  421. ret = _regmap_raw_read(map, reg, val, val_len);
  422. mutex_unlock(&map->lock);
  423. return ret;
  424. }
  425. EXPORT_SYMBOL_GPL(regmap_raw_read);
  426. /**
  427. * regmap_bulk_read(): Read multiple registers from the device
  428. *
  429. * @map: Register map to write to
  430. * @reg: First register to be read from
  431. * @val: Pointer to store read value, in native register size for device
  432. * @val_count: Number of registers to read
  433. *
  434. * A value of zero will be returned on success, a negative errno will
  435. * be returned in error cases.
  436. */
  437. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  438. size_t val_count)
  439. {
  440. int ret, i;
  441. size_t val_bytes = map->format.val_bytes;
  442. bool vol = true;
  443. if (!map->format.parse_val)
  444. return -EINVAL;
  445. /* Is this a block of volatile registers? */
  446. for (i = 0; i < val_count; i++)
  447. if (!regmap_volatile(map, reg + i))
  448. vol = false;
  449. if (vol || map->cache_type == REGCACHE_NONE) {
  450. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  451. if (ret != 0)
  452. return ret;
  453. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  454. map->format.parse_val(val + i);
  455. } else {
  456. for (i = 0; i < val_count; i++) {
  457. ret = regmap_read(map, reg + i, val + (i * val_bytes));
  458. if (ret != 0)
  459. return ret;
  460. }
  461. }
  462. return 0;
  463. }
  464. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  465. /**
  466. * remap_update_bits: Perform a read/modify/write cycle on the register map
  467. *
  468. * @map: Register map to update
  469. * @reg: Register to update
  470. * @mask: Bitmask to change
  471. * @val: New value for bitmask
  472. *
  473. * Returns zero for success, a negative number on error.
  474. */
  475. int regmap_update_bits(struct regmap *map, unsigned int reg,
  476. unsigned int mask, unsigned int val)
  477. {
  478. int ret;
  479. unsigned int tmp;
  480. mutex_lock(&map->lock);
  481. ret = _regmap_read(map, reg, &tmp);
  482. if (ret != 0)
  483. goto out;
  484. tmp &= ~mask;
  485. tmp |= val & mask;
  486. ret = _regmap_write(map, reg, tmp);
  487. out:
  488. mutex_unlock(&map->lock);
  489. return ret;
  490. }
  491. EXPORT_SYMBOL_GPL(regmap_update_bits);
  492. static int __init regmap_initcall(void)
  493. {
  494. regmap_debugfs_initcall();
  495. return 0;
  496. }
  497. postcore_initcall(regmap_initcall);