regmap.c 24 KB

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