regmap.c 17 KB

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