regmap.c 24 KB

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