regmap.c 16 KB

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