regmap.c 16 KB

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