regmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. if (val_len % map->format.val_bytes)
  494. return -EINVAL;
  495. map->lock(map);
  496. ret = _regmap_raw_write(map, reg, val, val_len);
  497. map->unlock(map);
  498. return ret;
  499. }
  500. EXPORT_SYMBOL_GPL(regmap_raw_write);
  501. /*
  502. * regmap_bulk_write(): Write multiple registers to the device
  503. *
  504. * @map: Register map to write to
  505. * @reg: First register to be write from
  506. * @val: Block of data to be written, in native register size for device
  507. * @val_count: Number of registers to write
  508. *
  509. * This function is intended to be used for writing a large block of
  510. * data to be device either in single transfer or multiple transfer.
  511. *
  512. * A value of zero will be returned on success, a negative errno will
  513. * be returned in error cases.
  514. */
  515. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  516. size_t val_count)
  517. {
  518. int ret = 0, i;
  519. size_t val_bytes = map->format.val_bytes;
  520. void *wval;
  521. if (!map->format.parse_val)
  522. return -EINVAL;
  523. map->lock(map);
  524. /* No formatting is require if val_byte is 1 */
  525. if (val_bytes == 1) {
  526. wval = (void *)val;
  527. } else {
  528. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  529. if (!wval) {
  530. ret = -ENOMEM;
  531. dev_err(map->dev, "Error in memory allocation\n");
  532. goto out;
  533. }
  534. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  535. map->format.parse_val(wval + i);
  536. }
  537. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  538. if (val_bytes != 1)
  539. kfree(wval);
  540. out:
  541. map->unlock(map);
  542. return ret;
  543. }
  544. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  545. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  546. unsigned int val_len)
  547. {
  548. u8 *u8 = map->work_buf;
  549. int ret;
  550. map->format.format_reg(map->work_buf, reg);
  551. /*
  552. * Some buses or devices flag reads by setting the high bits in the
  553. * register addresss; since it's always the high bits for all
  554. * current formats we can do this here rather than in
  555. * formatting. This may break if we get interesting formats.
  556. */
  557. u8[0] |= map->read_flag_mask;
  558. trace_regmap_hw_read_start(map->dev, reg,
  559. val_len / map->format.val_bytes);
  560. ret = map->bus->read(map->bus_context, map->work_buf,
  561. map->format.reg_bytes + map->format.pad_bytes,
  562. val, val_len);
  563. trace_regmap_hw_read_done(map->dev, reg,
  564. val_len / map->format.val_bytes);
  565. return ret;
  566. }
  567. static int _regmap_read(struct regmap *map, unsigned int reg,
  568. unsigned int *val)
  569. {
  570. int ret;
  571. if (!map->cache_bypass) {
  572. ret = regcache_read(map, reg, val);
  573. if (ret == 0)
  574. return 0;
  575. }
  576. if (!map->format.parse_val)
  577. return -EINVAL;
  578. if (map->cache_only)
  579. return -EBUSY;
  580. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  581. if (ret == 0) {
  582. *val = map->format.parse_val(map->work_buf);
  583. trace_regmap_reg_read(map->dev, reg, *val);
  584. }
  585. return ret;
  586. }
  587. /**
  588. * regmap_read(): Read a value from a single register
  589. *
  590. * @map: Register map to write to
  591. * @reg: Register to be read from
  592. * @val: Pointer to store read value
  593. *
  594. * A value of zero will be returned on success, a negative errno will
  595. * be returned in error cases.
  596. */
  597. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  598. {
  599. int ret;
  600. map->lock(map);
  601. ret = _regmap_read(map, reg, val);
  602. map->unlock(map);
  603. return ret;
  604. }
  605. EXPORT_SYMBOL_GPL(regmap_read);
  606. /**
  607. * regmap_raw_read(): Read raw data from the device
  608. *
  609. * @map: Register map to write to
  610. * @reg: First register to be read from
  611. * @val: Pointer to store read value
  612. * @val_len: Size of data to read
  613. *
  614. * A value of zero will be returned on success, a negative errno will
  615. * be returned in error cases.
  616. */
  617. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  618. size_t val_len)
  619. {
  620. size_t val_bytes = map->format.val_bytes;
  621. size_t val_count = val_len / val_bytes;
  622. unsigned int v;
  623. int ret, i;
  624. if (val_len % map->format.val_bytes)
  625. return -EINVAL;
  626. map->lock(map);
  627. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  628. map->cache_type == REGCACHE_NONE) {
  629. /* Physical block read if there's no cache involved */
  630. ret = _regmap_raw_read(map, reg, val, val_len);
  631. } else {
  632. /* Otherwise go word by word for the cache; should be low
  633. * cost as we expect to hit the cache.
  634. */
  635. for (i = 0; i < val_count; i++) {
  636. ret = _regmap_read(map, reg + i, &v);
  637. if (ret != 0)
  638. goto out;
  639. map->format.format_val(val + (i * val_bytes), v);
  640. }
  641. }
  642. out:
  643. map->unlock(map);
  644. return ret;
  645. }
  646. EXPORT_SYMBOL_GPL(regmap_raw_read);
  647. /**
  648. * regmap_bulk_read(): Read multiple registers from the device
  649. *
  650. * @map: Register map to write to
  651. * @reg: First register to be read from
  652. * @val: Pointer to store read value, in native register size for device
  653. * @val_count: Number of registers to read
  654. *
  655. * A value of zero will be returned on success, a negative errno will
  656. * be returned in error cases.
  657. */
  658. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  659. size_t val_count)
  660. {
  661. int ret, i;
  662. size_t val_bytes = map->format.val_bytes;
  663. bool vol = regmap_volatile_range(map, reg, val_count);
  664. if (!map->format.parse_val)
  665. return -EINVAL;
  666. if (vol || map->cache_type == REGCACHE_NONE) {
  667. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  668. if (ret != 0)
  669. return ret;
  670. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  671. map->format.parse_val(val + i);
  672. } else {
  673. for (i = 0; i < val_count; i++) {
  674. ret = regmap_read(map, reg + i, val + (i * val_bytes));
  675. if (ret != 0)
  676. return ret;
  677. }
  678. }
  679. return 0;
  680. }
  681. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  682. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  683. unsigned int mask, unsigned int val,
  684. bool *change)
  685. {
  686. int ret;
  687. unsigned int tmp, orig;
  688. map->lock(map);
  689. ret = _regmap_read(map, reg, &orig);
  690. if (ret != 0)
  691. goto out;
  692. tmp = orig & ~mask;
  693. tmp |= val & mask;
  694. if (tmp != orig) {
  695. ret = _regmap_write(map, reg, tmp);
  696. *change = true;
  697. } else {
  698. *change = false;
  699. }
  700. out:
  701. map->unlock(map);
  702. return ret;
  703. }
  704. /**
  705. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  706. *
  707. * @map: Register map to update
  708. * @reg: Register to update
  709. * @mask: Bitmask to change
  710. * @val: New value for bitmask
  711. *
  712. * Returns zero for success, a negative number on error.
  713. */
  714. int regmap_update_bits(struct regmap *map, unsigned int reg,
  715. unsigned int mask, unsigned int val)
  716. {
  717. bool change;
  718. return _regmap_update_bits(map, reg, mask, val, &change);
  719. }
  720. EXPORT_SYMBOL_GPL(regmap_update_bits);
  721. /**
  722. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  723. * register map and report if updated
  724. *
  725. * @map: Register map to update
  726. * @reg: Register to update
  727. * @mask: Bitmask to change
  728. * @val: New value for bitmask
  729. * @change: Boolean indicating if a write was done
  730. *
  731. * Returns zero for success, a negative number on error.
  732. */
  733. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  734. unsigned int mask, unsigned int val,
  735. bool *change)
  736. {
  737. return _regmap_update_bits(map, reg, mask, val, change);
  738. }
  739. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  740. /**
  741. * regmap_register_patch: Register and apply register updates to be applied
  742. * on device initialistion
  743. *
  744. * @map: Register map to apply updates to.
  745. * @regs: Values to update.
  746. * @num_regs: Number of entries in regs.
  747. *
  748. * Register a set of register updates to be applied to the device
  749. * whenever the device registers are synchronised with the cache and
  750. * apply them immediately. Typically this is used to apply
  751. * corrections to be applied to the device defaults on startup, such
  752. * as the updates some vendors provide to undocumented registers.
  753. */
  754. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  755. int num_regs)
  756. {
  757. int i, ret;
  758. bool bypass;
  759. /* If needed the implementation can be extended to support this */
  760. if (map->patch)
  761. return -EBUSY;
  762. map->lock(map);
  763. bypass = map->cache_bypass;
  764. map->cache_bypass = true;
  765. /* Write out first; it's useful to apply even if we fail later. */
  766. for (i = 0; i < num_regs; i++) {
  767. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  768. if (ret != 0) {
  769. dev_err(map->dev, "Failed to write %x = %x: %d\n",
  770. regs[i].reg, regs[i].def, ret);
  771. goto out;
  772. }
  773. }
  774. map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
  775. if (map->patch != NULL) {
  776. memcpy(map->patch, regs,
  777. num_regs * sizeof(struct reg_default));
  778. map->patch_regs = num_regs;
  779. } else {
  780. ret = -ENOMEM;
  781. }
  782. out:
  783. map->cache_bypass = bypass;
  784. map->unlock(map);
  785. return ret;
  786. }
  787. EXPORT_SYMBOL_GPL(regmap_register_patch);
  788. /*
  789. * regmap_get_val_bytes(): Report the size of a register value
  790. *
  791. * Report the size of a register value, mainly intended to for use by
  792. * generic infrastructure built on top of regmap.
  793. */
  794. int regmap_get_val_bytes(struct regmap *map)
  795. {
  796. if (map->format.format_write)
  797. return -EINVAL;
  798. return map->format.val_bytes;
  799. }
  800. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  801. static int __init regmap_initcall(void)
  802. {
  803. regmap_debugfs_initcall();
  804. return 0;
  805. }
  806. postcore_initcall(regmap_initcall);