regmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/err.h>
  16. #define CREATE_TRACE_POINTS
  17. #include <trace/events/regmap.h>
  18. #include "internal.h"
  19. static void regmap_format_4_12_write(struct regmap *map,
  20. unsigned int reg, unsigned int val)
  21. {
  22. __be16 *out = map->work_buf;
  23. *out = cpu_to_be16((reg << 12) | val);
  24. }
  25. static void regmap_format_7_9_write(struct regmap *map,
  26. unsigned int reg, unsigned int val)
  27. {
  28. __be16 *out = map->work_buf;
  29. *out = cpu_to_be16((reg << 9) | val);
  30. }
  31. static void regmap_format_8(void *buf, unsigned int val)
  32. {
  33. u8 *b = buf;
  34. b[0] = val;
  35. }
  36. static void regmap_format_16(void *buf, unsigned int val)
  37. {
  38. __be16 *b = buf;
  39. b[0] = cpu_to_be16(val);
  40. }
  41. static unsigned int regmap_parse_8(void *buf)
  42. {
  43. u8 *b = buf;
  44. return b[0];
  45. }
  46. static unsigned int regmap_parse_16(void *buf)
  47. {
  48. __be16 *b = buf;
  49. b[0] = be16_to_cpu(b[0]);
  50. return b[0];
  51. }
  52. /**
  53. * regmap_init(): Initialise register map
  54. *
  55. * @dev: Device that will be interacted with
  56. * @bus: Bus-specific callbacks to use with device
  57. * @config: Configuration for register map
  58. *
  59. * The return value will be an ERR_PTR() on error or a valid pointer to
  60. * a struct regmap. This function should generally not be called
  61. * directly, it should be called by bus-specific init functions.
  62. */
  63. struct regmap *regmap_init(struct device *dev,
  64. const struct regmap_bus *bus,
  65. const struct regmap_config *config)
  66. {
  67. struct regmap *map;
  68. int ret = -EINVAL;
  69. if (!bus || !config)
  70. return NULL;
  71. map = kzalloc(sizeof(*map), GFP_KERNEL);
  72. if (map == NULL) {
  73. ret = -ENOMEM;
  74. goto err;
  75. }
  76. mutex_init(&map->lock);
  77. map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
  78. map->format.reg_bytes = config->reg_bits / 8;
  79. map->format.val_bytes = config->val_bits / 8;
  80. map->dev = dev;
  81. map->bus = bus;
  82. map->max_register = config->max_register;
  83. map->writeable_reg = config->writeable_reg;
  84. map->readable_reg = config->readable_reg;
  85. map->volatile_reg = config->volatile_reg;
  86. switch (config->reg_bits) {
  87. case 4:
  88. switch (config->val_bits) {
  89. case 12:
  90. map->format.format_write = regmap_format_4_12_write;
  91. break;
  92. default:
  93. goto err_map;
  94. }
  95. break;
  96. case 7:
  97. switch (config->val_bits) {
  98. case 9:
  99. map->format.format_write = regmap_format_7_9_write;
  100. break;
  101. default:
  102. goto err_map;
  103. }
  104. break;
  105. case 8:
  106. map->format.format_reg = regmap_format_8;
  107. break;
  108. case 16:
  109. map->format.format_reg = regmap_format_16;
  110. break;
  111. default:
  112. goto err_map;
  113. }
  114. switch (config->val_bits) {
  115. case 8:
  116. map->format.format_val = regmap_format_8;
  117. map->format.parse_val = regmap_parse_8;
  118. break;
  119. case 16:
  120. map->format.format_val = regmap_format_16;
  121. map->format.parse_val = regmap_parse_16;
  122. break;
  123. }
  124. if (!map->format.format_write &&
  125. !(map->format.format_reg && map->format.format_val))
  126. goto err_map;
  127. map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
  128. if (map->work_buf == NULL) {
  129. ret = -ENOMEM;
  130. goto err_bus;
  131. }
  132. return map;
  133. err_bus:
  134. module_put(map->bus->owner);
  135. err_map:
  136. kfree(map);
  137. err:
  138. return ERR_PTR(ret);
  139. }
  140. EXPORT_SYMBOL_GPL(regmap_init);
  141. /**
  142. * regmap_exit(): Free a previously allocated register map
  143. */
  144. void regmap_exit(struct regmap *map)
  145. {
  146. kfree(map->work_buf);
  147. module_put(map->bus->owner);
  148. kfree(map);
  149. }
  150. EXPORT_SYMBOL_GPL(regmap_exit);
  151. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  152. const void *val, size_t val_len)
  153. {
  154. void *buf;
  155. int ret = -ENOTSUPP;
  156. size_t len;
  157. int i;
  158. /* Check for unwritable registers before we start */
  159. if (map->writeable_reg)
  160. for (i = 0; i < val_len / map->format.val_bytes; i++)
  161. if (!map->writeable_reg(map->dev, reg + i))
  162. return -EINVAL;
  163. map->format.format_reg(map->work_buf, reg);
  164. trace_regmap_hw_write_start(map->dev, reg,
  165. val_len / map->format.val_bytes);
  166. /* If we're doing a single register write we can probably just
  167. * send the work_buf directly, otherwise try to do a gather
  168. * write.
  169. */
  170. if (val == map->work_buf + map->format.reg_bytes)
  171. ret = map->bus->write(map->dev, map->work_buf,
  172. map->format.reg_bytes + val_len);
  173. else if (map->bus->gather_write)
  174. ret = map->bus->gather_write(map->dev, map->work_buf,
  175. map->format.reg_bytes,
  176. val, val_len);
  177. /* If that didn't work fall back on linearising by hand. */
  178. if (ret == -ENOTSUPP) {
  179. len = map->format.reg_bytes + val_len;
  180. buf = kmalloc(len, GFP_KERNEL);
  181. if (!buf)
  182. return -ENOMEM;
  183. memcpy(buf, map->work_buf, map->format.reg_bytes);
  184. memcpy(buf + map->format.reg_bytes, val, val_len);
  185. ret = map->bus->write(map->dev, buf, len);
  186. kfree(buf);
  187. }
  188. trace_regmap_hw_write_done(map->dev, reg,
  189. val_len / map->format.val_bytes);
  190. return ret;
  191. }
  192. static int _regmap_write(struct regmap *map, unsigned int reg,
  193. unsigned int val)
  194. {
  195. int ret;
  196. BUG_ON(!map->format.format_write && !map->format.format_val);
  197. trace_regmap_reg_write(map->dev, reg, val);
  198. if (map->format.format_write) {
  199. map->format.format_write(map, reg, val);
  200. trace_regmap_hw_write_start(map->dev, reg, 1);
  201. ret = map->bus->write(map->dev, map->work_buf,
  202. map->format.buf_size);
  203. trace_regmap_hw_write_done(map->dev, reg, 1);
  204. return ret;
  205. } else {
  206. map->format.format_val(map->work_buf + map->format.reg_bytes,
  207. val);
  208. return _regmap_raw_write(map, reg,
  209. map->work_buf + map->format.reg_bytes,
  210. map->format.val_bytes);
  211. }
  212. }
  213. /**
  214. * regmap_write(): Write a value to a single register
  215. *
  216. * @map: Register map to write to
  217. * @reg: Register to write to
  218. * @val: Value to be written
  219. *
  220. * A value of zero will be returned on success, a negative errno will
  221. * be returned in error cases.
  222. */
  223. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  224. {
  225. int ret;
  226. mutex_lock(&map->lock);
  227. ret = _regmap_write(map, reg, val);
  228. mutex_unlock(&map->lock);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(regmap_write);
  232. /**
  233. * regmap_raw_write(): Write raw values to one or more registers
  234. *
  235. * @map: Register map to write to
  236. * @reg: Initial register to write to
  237. * @val: Block of data to be written, laid out for direct transmission to the
  238. * device
  239. * @val_len: Length of data pointed to by val.
  240. *
  241. * This function is intended to be used for things like firmware
  242. * download where a large block of data needs to be transferred to the
  243. * device. No formatting will be done on the data provided.
  244. *
  245. * A value of zero will be returned on success, a negative errno will
  246. * be returned in error cases.
  247. */
  248. int regmap_raw_write(struct regmap *map, unsigned int reg,
  249. const void *val, size_t val_len)
  250. {
  251. int ret;
  252. mutex_lock(&map->lock);
  253. ret = _regmap_raw_write(map, reg, val, val_len);
  254. mutex_unlock(&map->lock);
  255. return ret;
  256. }
  257. EXPORT_SYMBOL_GPL(regmap_raw_write);
  258. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  259. unsigned int val_len)
  260. {
  261. u8 *u8 = map->work_buf;
  262. int ret;
  263. map->format.format_reg(map->work_buf, reg);
  264. /*
  265. * Some buses flag reads by setting the high bits in the
  266. * register addresss; since it's always the high bits for all
  267. * current formats we can do this here rather than in
  268. * formatting. This may break if we get interesting formats.
  269. */
  270. if (map->bus->read_flag_mask)
  271. u8[0] |= map->bus->read_flag_mask;
  272. trace_regmap_hw_read_start(map->dev, reg,
  273. val_len / map->format.val_bytes);
  274. ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
  275. val, val_len);
  276. trace_regmap_hw_read_done(map->dev, reg,
  277. val_len / map->format.val_bytes);
  278. return ret;
  279. }
  280. static int _regmap_read(struct regmap *map, unsigned int reg,
  281. unsigned int *val)
  282. {
  283. int ret;
  284. if (!map->format.parse_val)
  285. return -EINVAL;
  286. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  287. if (ret == 0) {
  288. *val = map->format.parse_val(map->work_buf);
  289. trace_regmap_reg_read(map->dev, reg, *val);
  290. }
  291. return ret;
  292. }
  293. /**
  294. * regmap_read(): Read a value from a single register
  295. *
  296. * @map: Register map to write to
  297. * @reg: Register to be read from
  298. * @val: Pointer to store read value
  299. *
  300. * A value of zero will be returned on success, a negative errno will
  301. * be returned in error cases.
  302. */
  303. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  304. {
  305. int ret;
  306. mutex_lock(&map->lock);
  307. ret = _regmap_read(map, reg, val);
  308. mutex_unlock(&map->lock);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL_GPL(regmap_read);
  312. /**
  313. * regmap_raw_read(): Read raw data from the device
  314. *
  315. * @map: Register map to write to
  316. * @reg: First register to be read from
  317. * @val: Pointer to store read value
  318. * @val_len: Size of data to read
  319. *
  320. * A value of zero will be returned on success, a negative errno will
  321. * be returned in error cases.
  322. */
  323. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  324. size_t val_len)
  325. {
  326. int ret;
  327. mutex_lock(&map->lock);
  328. ret = _regmap_raw_read(map, reg, val, val_len);
  329. mutex_unlock(&map->lock);
  330. return ret;
  331. }
  332. EXPORT_SYMBOL_GPL(regmap_raw_read);
  333. /**
  334. * regmap_bulk_read(): Read multiple registers from the device
  335. *
  336. * @map: Register map to write to
  337. * @reg: First register to be read from
  338. * @val: Pointer to store read value, in native register size for device
  339. * @val_count: Number of registers to read
  340. *
  341. * A value of zero will be returned on success, a negative errno will
  342. * be returned in error cases.
  343. */
  344. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  345. size_t val_count)
  346. {
  347. int ret, i;
  348. size_t val_bytes = map->format.val_bytes;
  349. if (!map->format.parse_val)
  350. return -EINVAL;
  351. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  352. if (ret != 0)
  353. return ret;
  354. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  355. map->format.parse_val(val + i);
  356. return 0;
  357. }
  358. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  359. /**
  360. * remap_update_bits: Perform a read/modify/write cycle on the register map
  361. *
  362. * @map: Register map to update
  363. * @reg: Register to update
  364. * @mask: Bitmask to change
  365. * @val: New value for bitmask
  366. *
  367. * Returns zero for success, a negative number on error.
  368. */
  369. int regmap_update_bits(struct regmap *map, unsigned int reg,
  370. unsigned int mask, unsigned int val)
  371. {
  372. int ret;
  373. unsigned int tmp;
  374. mutex_lock(&map->lock);
  375. ret = _regmap_read(map, reg, &tmp);
  376. if (ret != 0)
  377. goto out;
  378. tmp &= ~mask;
  379. tmp |= val & mask;
  380. ret = _regmap_write(map, reg, tmp);
  381. out:
  382. mutex_unlock(&map->lock);
  383. return ret;
  384. }
  385. EXPORT_SYMBOL_GPL(regmap_update_bits);