regmap.c 9.8 KB

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