regmap.c 22 KB

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