regmap.c 15 KB

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