regmap.c 22 KB

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