regmap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/export.h>
  15. #include <linux/mutex.h>
  16. #include <linux/err.h>
  17. #define CREATE_TRACE_POINTS
  18. #include <trace/events/regmap.h>
  19. #include "internal.h"
  20. bool regmap_writeable(struct regmap *map, unsigned int reg)
  21. {
  22. if (map->max_register && reg > map->max_register)
  23. return false;
  24. if (map->writeable_reg)
  25. return map->writeable_reg(map->dev, reg);
  26. return true;
  27. }
  28. bool regmap_readable(struct regmap *map, unsigned int reg)
  29. {
  30. if (map->max_register && reg > map->max_register)
  31. return false;
  32. if (map->format.format_write)
  33. return false;
  34. if (map->readable_reg)
  35. return map->readable_reg(map->dev, reg);
  36. return true;
  37. }
  38. bool regmap_volatile(struct regmap *map, unsigned int reg)
  39. {
  40. if (!map->format.format_write && !regmap_readable(map, reg))
  41. return false;
  42. if (map->volatile_reg)
  43. return map->volatile_reg(map->dev, reg);
  44. return true;
  45. }
  46. bool regmap_precious(struct regmap *map, unsigned int reg)
  47. {
  48. if (!regmap_readable(map, reg))
  49. return false;
  50. if (map->precious_reg)
  51. return map->precious_reg(map->dev, reg);
  52. return false;
  53. }
  54. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  55. size_t num)
  56. {
  57. unsigned int i;
  58. for (i = 0; i < num; i++)
  59. if (!regmap_volatile(map, reg + i))
  60. return false;
  61. return true;
  62. }
  63. static void regmap_format_2_6_write(struct regmap *map,
  64. unsigned int reg, unsigned int val)
  65. {
  66. u8 *out = map->work_buf;
  67. *out = (reg << 6) | val;
  68. }
  69. static void regmap_format_4_12_write(struct regmap *map,
  70. unsigned int reg, unsigned int val)
  71. {
  72. __be16 *out = map->work_buf;
  73. *out = cpu_to_be16((reg << 12) | val);
  74. }
  75. static void regmap_format_7_9_write(struct regmap *map,
  76. unsigned int reg, unsigned int val)
  77. {
  78. __be16 *out = map->work_buf;
  79. *out = cpu_to_be16((reg << 9) | val);
  80. }
  81. static void regmap_format_10_14_write(struct regmap *map,
  82. unsigned int reg, unsigned int val)
  83. {
  84. u8 *out = map->work_buf;
  85. out[2] = val;
  86. out[1] = (val >> 8) | (reg << 6);
  87. out[0] = reg >> 2;
  88. }
  89. static void regmap_format_8(void *buf, unsigned int val)
  90. {
  91. u8 *b = buf;
  92. b[0] = val;
  93. }
  94. static void regmap_format_16(void *buf, unsigned int val)
  95. {
  96. __be16 *b = buf;
  97. b[0] = cpu_to_be16(val);
  98. }
  99. static void regmap_format_32(void *buf, unsigned int val)
  100. {
  101. __be32 *b = buf;
  102. b[0] = cpu_to_be32(val);
  103. }
  104. static unsigned int regmap_parse_8(void *buf)
  105. {
  106. u8 *b = buf;
  107. return b[0];
  108. }
  109. static unsigned int regmap_parse_16(void *buf)
  110. {
  111. __be16 *b = buf;
  112. b[0] = be16_to_cpu(b[0]);
  113. return b[0];
  114. }
  115. static unsigned int regmap_parse_32(void *buf)
  116. {
  117. __be32 *b = buf;
  118. b[0] = be32_to_cpu(b[0]);
  119. return b[0];
  120. }
  121. /**
  122. * regmap_init(): Initialise register map
  123. *
  124. * @dev: Device that will be interacted with
  125. * @bus: Bus-specific callbacks to use with device
  126. * @config: Configuration for register map
  127. *
  128. * The return value will be an ERR_PTR() on error or a valid pointer to
  129. * a struct regmap. This function should generally not be called
  130. * directly, it should be called by bus-specific init functions.
  131. */
  132. struct regmap *regmap_init(struct device *dev,
  133. const struct regmap_bus *bus,
  134. const struct regmap_config *config)
  135. {
  136. struct regmap *map;
  137. int ret = -EINVAL;
  138. if (!bus || !config)
  139. goto err;
  140. map = kzalloc(sizeof(*map), GFP_KERNEL);
  141. if (map == NULL) {
  142. ret = -ENOMEM;
  143. goto err;
  144. }
  145. mutex_init(&map->lock);
  146. map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
  147. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  148. map->format.pad_bytes = config->pad_bits / 8;
  149. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  150. map->format.buf_size += map->format.pad_bytes;
  151. map->dev = dev;
  152. map->bus = bus;
  153. map->max_register = config->max_register;
  154. map->writeable_reg = config->writeable_reg;
  155. map->readable_reg = config->readable_reg;
  156. map->volatile_reg = config->volatile_reg;
  157. map->precious_reg = config->precious_reg;
  158. map->cache_type = config->cache_type;
  159. if (config->read_flag_mask || config->write_flag_mask) {
  160. map->read_flag_mask = config->read_flag_mask;
  161. map->write_flag_mask = config->write_flag_mask;
  162. } else {
  163. map->read_flag_mask = bus->read_flag_mask;
  164. }
  165. switch (config->reg_bits) {
  166. case 2:
  167. switch (config->val_bits) {
  168. case 6:
  169. map->format.format_write = regmap_format_2_6_write;
  170. break;
  171. default:
  172. goto err_map;
  173. }
  174. break;
  175. case 4:
  176. switch (config->val_bits) {
  177. case 12:
  178. map->format.format_write = regmap_format_4_12_write;
  179. break;
  180. default:
  181. goto err_map;
  182. }
  183. break;
  184. case 7:
  185. switch (config->val_bits) {
  186. case 9:
  187. map->format.format_write = regmap_format_7_9_write;
  188. break;
  189. default:
  190. goto err_map;
  191. }
  192. break;
  193. case 10:
  194. switch (config->val_bits) {
  195. case 14:
  196. map->format.format_write = regmap_format_10_14_write;
  197. break;
  198. default:
  199. goto err_map;
  200. }
  201. break;
  202. case 8:
  203. map->format.format_reg = regmap_format_8;
  204. break;
  205. case 16:
  206. map->format.format_reg = regmap_format_16;
  207. break;
  208. case 32:
  209. map->format.format_reg = regmap_format_32;
  210. break;
  211. default:
  212. goto err_map;
  213. }
  214. switch (config->val_bits) {
  215. case 8:
  216. map->format.format_val = regmap_format_8;
  217. map->format.parse_val = regmap_parse_8;
  218. break;
  219. case 16:
  220. map->format.format_val = regmap_format_16;
  221. map->format.parse_val = regmap_parse_16;
  222. break;
  223. case 32:
  224. map->format.format_val = regmap_format_32;
  225. map->format.parse_val = regmap_parse_32;
  226. break;
  227. }
  228. if (!map->format.format_write &&
  229. !(map->format.format_reg && map->format.format_val))
  230. goto err_map;
  231. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  232. if (map->work_buf == NULL) {
  233. ret = -ENOMEM;
  234. goto err_map;
  235. }
  236. regmap_debugfs_init(map);
  237. ret = regcache_init(map, config);
  238. if (ret < 0)
  239. goto err_free_workbuf;
  240. return map;
  241. err_free_workbuf:
  242. kfree(map->work_buf);
  243. err_map:
  244. kfree(map);
  245. err:
  246. return ERR_PTR(ret);
  247. }
  248. EXPORT_SYMBOL_GPL(regmap_init);
  249. static void devm_regmap_release(struct device *dev, void *res)
  250. {
  251. regmap_exit(*(struct regmap **)res);
  252. }
  253. /**
  254. * devm_regmap_init(): Initialise managed register map
  255. *
  256. * @dev: Device that will be interacted with
  257. * @bus: Bus-specific callbacks to use with device
  258. * @config: Configuration for register map
  259. *
  260. * The return value will be an ERR_PTR() on error or a valid pointer
  261. * to a struct regmap. This function should generally not be called
  262. * directly, it should be called by bus-specific init functions. The
  263. * map will be automatically freed by the device management code.
  264. */
  265. struct regmap *devm_regmap_init(struct device *dev,
  266. const struct regmap_bus *bus,
  267. const struct regmap_config *config)
  268. {
  269. struct regmap **ptr, *regmap;
  270. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  271. if (!ptr)
  272. return ERR_PTR(-ENOMEM);
  273. regmap = regmap_init(dev, bus, config);
  274. if (!IS_ERR(regmap)) {
  275. *ptr = regmap;
  276. devres_add(dev, ptr);
  277. } else {
  278. devres_free(ptr);
  279. }
  280. return regmap;
  281. }
  282. EXPORT_SYMBOL_GPL(devm_regmap_init);
  283. /**
  284. * regmap_reinit_cache(): Reinitialise the current register cache
  285. *
  286. * @map: Register map to operate on.
  287. * @config: New configuration. Only the cache data will be used.
  288. *
  289. * Discard any existing register cache for the map and initialize a
  290. * new cache. This can be used to restore the cache to defaults or to
  291. * update the cache configuration to reflect runtime discovery of the
  292. * hardware.
  293. */
  294. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  295. {
  296. int ret;
  297. mutex_lock(&map->lock);
  298. regcache_exit(map);
  299. regmap_debugfs_exit(map);
  300. map->max_register = config->max_register;
  301. map->writeable_reg = config->writeable_reg;
  302. map->readable_reg = config->readable_reg;
  303. map->volatile_reg = config->volatile_reg;
  304. map->precious_reg = config->precious_reg;
  305. map->cache_type = config->cache_type;
  306. regmap_debugfs_init(map);
  307. map->cache_bypass = false;
  308. map->cache_only = false;
  309. ret = regcache_init(map, config);
  310. mutex_unlock(&map->lock);
  311. return ret;
  312. }
  313. /**
  314. * regmap_exit(): Free a previously allocated register map
  315. */
  316. void regmap_exit(struct regmap *map)
  317. {
  318. regcache_exit(map);
  319. regmap_debugfs_exit(map);
  320. kfree(map->work_buf);
  321. kfree(map);
  322. }
  323. EXPORT_SYMBOL_GPL(regmap_exit);
  324. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  325. const void *val, size_t val_len)
  326. {
  327. u8 *u8 = map->work_buf;
  328. void *buf;
  329. int ret = -ENOTSUPP;
  330. size_t len;
  331. int i;
  332. /* Check for unwritable registers before we start */
  333. if (map->writeable_reg)
  334. for (i = 0; i < val_len / map->format.val_bytes; i++)
  335. if (!map->writeable_reg(map->dev, reg + i))
  336. return -EINVAL;
  337. if (!map->cache_bypass && map->format.parse_val) {
  338. unsigned int ival;
  339. int val_bytes = map->format.val_bytes;
  340. for (i = 0; i < val_len / val_bytes; i++) {
  341. memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
  342. ival = map->format.parse_val(map->work_buf);
  343. ret = regcache_write(map, reg + i, ival);
  344. if (ret) {
  345. dev_err(map->dev,
  346. "Error in caching of register: %u ret: %d\n",
  347. reg + i, ret);
  348. return ret;
  349. }
  350. }
  351. if (map->cache_only) {
  352. map->cache_dirty = true;
  353. return 0;
  354. }
  355. }
  356. map->format.format_reg(map->work_buf, reg);
  357. u8[0] |= map->write_flag_mask;
  358. trace_regmap_hw_write_start(map->dev, reg,
  359. val_len / map->format.val_bytes);
  360. /* If we're doing a single register write we can probably just
  361. * send the work_buf directly, otherwise try to do a gather
  362. * write.
  363. */
  364. if (val == (map->work_buf + map->format.pad_bytes +
  365. map->format.reg_bytes))
  366. ret = map->bus->write(map->dev, map->work_buf,
  367. map->format.reg_bytes +
  368. map->format.pad_bytes +
  369. val_len);
  370. else if (map->bus->gather_write)
  371. ret = map->bus->gather_write(map->dev, map->work_buf,
  372. map->format.reg_bytes +
  373. map->format.pad_bytes,
  374. val, val_len);
  375. /* If that didn't work fall back on linearising by hand. */
  376. if (ret == -ENOTSUPP) {
  377. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  378. buf = kzalloc(len, GFP_KERNEL);
  379. if (!buf)
  380. return -ENOMEM;
  381. memcpy(buf, map->work_buf, map->format.reg_bytes);
  382. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  383. val, val_len);
  384. ret = map->bus->write(map->dev, buf, len);
  385. kfree(buf);
  386. }
  387. trace_regmap_hw_write_done(map->dev, reg,
  388. val_len / map->format.val_bytes);
  389. return ret;
  390. }
  391. int _regmap_write(struct regmap *map, unsigned int reg,
  392. unsigned int val)
  393. {
  394. int ret;
  395. BUG_ON(!map->format.format_write && !map->format.format_val);
  396. if (!map->cache_bypass && map->format.format_write) {
  397. ret = regcache_write(map, reg, val);
  398. if (ret != 0)
  399. return ret;
  400. if (map->cache_only) {
  401. map->cache_dirty = true;
  402. return 0;
  403. }
  404. }
  405. trace_regmap_reg_write(map->dev, reg, val);
  406. if (map->format.format_write) {
  407. map->format.format_write(map, reg, val);
  408. trace_regmap_hw_write_start(map->dev, reg, 1);
  409. ret = map->bus->write(map->dev, map->work_buf,
  410. map->format.buf_size);
  411. trace_regmap_hw_write_done(map->dev, reg, 1);
  412. return ret;
  413. } else {
  414. map->format.format_val(map->work_buf + map->format.reg_bytes
  415. + map->format.pad_bytes, val);
  416. return _regmap_raw_write(map, reg,
  417. map->work_buf +
  418. map->format.reg_bytes +
  419. map->format.pad_bytes,
  420. map->format.val_bytes);
  421. }
  422. }
  423. /**
  424. * regmap_write(): Write a value to a single register
  425. *
  426. * @map: Register map to write to
  427. * @reg: Register to write to
  428. * @val: Value to be written
  429. *
  430. * A value of zero will be returned on success, a negative errno will
  431. * be returned in error cases.
  432. */
  433. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  434. {
  435. int ret;
  436. mutex_lock(&map->lock);
  437. ret = _regmap_write(map, reg, val);
  438. mutex_unlock(&map->lock);
  439. return ret;
  440. }
  441. EXPORT_SYMBOL_GPL(regmap_write);
  442. /**
  443. * regmap_raw_write(): Write raw values to one or more registers
  444. *
  445. * @map: Register map to write to
  446. * @reg: Initial register to write to
  447. * @val: Block of data to be written, laid out for direct transmission to the
  448. * device
  449. * @val_len: Length of data pointed to by val.
  450. *
  451. * This function is intended to be used for things like firmware
  452. * download where a large block of data needs to be transferred to the
  453. * device. No formatting will be done on the data provided.
  454. *
  455. * A value of zero will be returned on success, a negative errno will
  456. * be returned in error cases.
  457. */
  458. int regmap_raw_write(struct regmap *map, unsigned int reg,
  459. const void *val, size_t val_len)
  460. {
  461. int ret;
  462. mutex_lock(&map->lock);
  463. ret = _regmap_raw_write(map, reg, val, val_len);
  464. mutex_unlock(&map->lock);
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(regmap_raw_write);
  468. /*
  469. * regmap_bulk_write(): Write multiple registers to the device
  470. *
  471. * @map: Register map to write to
  472. * @reg: First register to be write from
  473. * @val: Block of data to be written, in native register size for device
  474. * @val_count: Number of registers to write
  475. *
  476. * This function is intended to be used for writing a large block of
  477. * data to be device either in single transfer or multiple transfer.
  478. *
  479. * A value of zero will be returned on success, a negative errno will
  480. * be returned in error cases.
  481. */
  482. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  483. size_t val_count)
  484. {
  485. int ret = 0, i;
  486. size_t val_bytes = map->format.val_bytes;
  487. void *wval;
  488. if (!map->format.parse_val)
  489. return -EINVAL;
  490. mutex_lock(&map->lock);
  491. /* No formatting is require if val_byte is 1 */
  492. if (val_bytes == 1) {
  493. wval = (void *)val;
  494. } else {
  495. if (!val_count) {
  496. ret = -EINVAL;
  497. goto out;
  498. }
  499. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  500. if (!wval) {
  501. ret = -ENOMEM;
  502. dev_err(map->dev, "Error in memory allocation\n");
  503. goto out;
  504. }
  505. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  506. map->format.parse_val(wval + i);
  507. }
  508. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  509. if (val_bytes != 1)
  510. kfree(wval);
  511. out:
  512. mutex_unlock(&map->lock);
  513. return ret;
  514. }
  515. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  516. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  517. unsigned int val_len)
  518. {
  519. u8 *u8 = map->work_buf;
  520. int ret;
  521. map->format.format_reg(map->work_buf, reg);
  522. /*
  523. * Some buses or devices flag reads by setting the high bits in the
  524. * register addresss; since it's always the high bits for all
  525. * current formats we can do this here rather than in
  526. * formatting. This may break if we get interesting formats.
  527. */
  528. u8[0] |= map->read_flag_mask;
  529. trace_regmap_hw_read_start(map->dev, reg,
  530. val_len / map->format.val_bytes);
  531. ret = map->bus->read(map->dev, map->work_buf,
  532. map->format.reg_bytes + map->format.pad_bytes,
  533. val, val_len);
  534. trace_regmap_hw_read_done(map->dev, reg,
  535. val_len / map->format.val_bytes);
  536. return ret;
  537. }
  538. static int _regmap_read(struct regmap *map, unsigned int reg,
  539. unsigned int *val)
  540. {
  541. int ret;
  542. if (!map->cache_bypass) {
  543. ret = regcache_read(map, reg, val);
  544. if (ret == 0)
  545. return 0;
  546. }
  547. if (!map->format.parse_val)
  548. return -EINVAL;
  549. if (map->cache_only)
  550. return -EBUSY;
  551. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  552. if (ret == 0) {
  553. *val = map->format.parse_val(map->work_buf);
  554. trace_regmap_reg_read(map->dev, reg, *val);
  555. }
  556. return ret;
  557. }
  558. /**
  559. * regmap_read(): Read a value from a single register
  560. *
  561. * @map: Register map to write to
  562. * @reg: Register to be read from
  563. * @val: Pointer to store read value
  564. *
  565. * A value of zero will be returned on success, a negative errno will
  566. * be returned in error cases.
  567. */
  568. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  569. {
  570. int ret;
  571. mutex_lock(&map->lock);
  572. ret = _regmap_read(map, reg, val);
  573. mutex_unlock(&map->lock);
  574. return ret;
  575. }
  576. EXPORT_SYMBOL_GPL(regmap_read);
  577. /**
  578. * regmap_raw_read(): Read raw data from the device
  579. *
  580. * @map: Register map to write to
  581. * @reg: First register to be read from
  582. * @val: Pointer to store read value
  583. * @val_len: Size of data to read
  584. *
  585. * A value of zero will be returned on success, a negative errno will
  586. * be returned in error cases.
  587. */
  588. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  589. size_t val_len)
  590. {
  591. size_t val_bytes = map->format.val_bytes;
  592. size_t val_count = val_len / val_bytes;
  593. unsigned int v;
  594. int ret, i;
  595. mutex_lock(&map->lock);
  596. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  597. map->cache_type == REGCACHE_NONE) {
  598. /* Physical block read if there's no cache involved */
  599. ret = _regmap_raw_read(map, reg, val, val_len);
  600. } else {
  601. /* Otherwise go word by word for the cache; should be low
  602. * cost as we expect to hit the cache.
  603. */
  604. for (i = 0; i < val_count; i++) {
  605. ret = _regmap_read(map, reg + i, &v);
  606. if (ret != 0)
  607. goto out;
  608. map->format.format_val(val + (i * val_bytes), v);
  609. }
  610. }
  611. out:
  612. mutex_unlock(&map->lock);
  613. return ret;
  614. }
  615. EXPORT_SYMBOL_GPL(regmap_raw_read);
  616. /**
  617. * regmap_bulk_read(): Read multiple registers from the device
  618. *
  619. * @map: Register map to write to
  620. * @reg: First register to be read from
  621. * @val: Pointer to store read value, in native register size for device
  622. * @val_count: Number of registers to read
  623. *
  624. * A value of zero will be returned on success, a negative errno will
  625. * be returned in error cases.
  626. */
  627. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  628. size_t val_count)
  629. {
  630. int ret, i;
  631. size_t val_bytes = map->format.val_bytes;
  632. bool vol = regmap_volatile_range(map, reg, val_count);
  633. if (!map->format.parse_val)
  634. return -EINVAL;
  635. if (vol || map->cache_type == REGCACHE_NONE) {
  636. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  637. if (ret != 0)
  638. return ret;
  639. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  640. map->format.parse_val(val + i);
  641. } else {
  642. for (i = 0; i < val_count; i++) {
  643. unsigned int ival;
  644. ret = regmap_read(map, reg + i, &ival);
  645. if (ret != 0)
  646. return ret;
  647. map->format.format_val(val + (i * val_bytes), ival);
  648. }
  649. }
  650. return 0;
  651. }
  652. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  653. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  654. unsigned int mask, unsigned int val,
  655. bool *change)
  656. {
  657. int ret;
  658. unsigned int tmp, orig;
  659. mutex_lock(&map->lock);
  660. ret = _regmap_read(map, reg, &orig);
  661. if (ret != 0)
  662. goto out;
  663. tmp = orig & ~mask;
  664. tmp |= val & mask;
  665. if (tmp != orig) {
  666. ret = _regmap_write(map, reg, tmp);
  667. *change = true;
  668. } else {
  669. *change = false;
  670. }
  671. out:
  672. mutex_unlock(&map->lock);
  673. return ret;
  674. }
  675. /**
  676. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  677. *
  678. * @map: Register map to update
  679. * @reg: Register to update
  680. * @mask: Bitmask to change
  681. * @val: New value for bitmask
  682. *
  683. * Returns zero for success, a negative number on error.
  684. */
  685. int regmap_update_bits(struct regmap *map, unsigned int reg,
  686. unsigned int mask, unsigned int val)
  687. {
  688. bool change;
  689. return _regmap_update_bits(map, reg, mask, val, &change);
  690. }
  691. EXPORT_SYMBOL_GPL(regmap_update_bits);
  692. /**
  693. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  694. * register map and report if updated
  695. *
  696. * @map: Register map to update
  697. * @reg: Register to update
  698. * @mask: Bitmask to change
  699. * @val: New value for bitmask
  700. * @change: Boolean indicating if a write was done
  701. *
  702. * Returns zero for success, a negative number on error.
  703. */
  704. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  705. unsigned int mask, unsigned int val,
  706. bool *change)
  707. {
  708. return _regmap_update_bits(map, reg, mask, val, change);
  709. }
  710. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  711. /**
  712. * regmap_register_patch: Register and apply register updates to be applied
  713. * on device initialistion
  714. *
  715. * @map: Register map to apply updates to.
  716. * @regs: Values to update.
  717. * @num_regs: Number of entries in regs.
  718. *
  719. * Register a set of register updates to be applied to the device
  720. * whenever the device registers are synchronised with the cache and
  721. * apply them immediately. Typically this is used to apply
  722. * corrections to be applied to the device defaults on startup, such
  723. * as the updates some vendors provide to undocumented registers.
  724. */
  725. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  726. int num_regs)
  727. {
  728. int i, ret;
  729. bool bypass;
  730. /* If needed the implementation can be extended to support this */
  731. if (map->patch)
  732. return -EBUSY;
  733. mutex_lock(&map->lock);
  734. bypass = map->cache_bypass;
  735. map->cache_bypass = true;
  736. /* Write out first; it's useful to apply even if we fail later. */
  737. for (i = 0; i < num_regs; i++) {
  738. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  739. if (ret != 0) {
  740. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  741. regs[i].reg, regs[i].def, ret);
  742. goto out;
  743. }
  744. }
  745. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  746. if (map->patch != NULL) {
  747. memcpy(map->patch, regs,
  748. num_regs * sizeof(struct reg_default));
  749. map->patch_regs = num_regs;
  750. } else {
  751. ret = -ENOMEM;
  752. }
  753. out:
  754. map->cache_bypass = bypass;
  755. mutex_unlock(&map->lock);
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(regmap_register_patch);
  759. /*
  760. * regmap_get_val_bytes(): Report the size of a register value
  761. *
  762. * Report the size of a register value, mainly intended to for use by
  763. * generic infrastructure built on top of regmap.
  764. */
  765. int regmap_get_val_bytes(struct regmap *map)
  766. {
  767. if (map->format.format_write)
  768. return -EINVAL;
  769. return map->format.val_bytes;
  770. }
  771. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  772. static int __init regmap_initcall(void)
  773. {
  774. regmap_debugfs_initcall();
  775. return 0;
  776. }
  777. postcore_initcall(regmap_initcall);