regmap.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_bus;
  143. }
  144. return map;
  145. err_bus:
  146. module_put(map->bus->owner);
  147. err_map:
  148. kfree(map);
  149. err:
  150. return ERR_PTR(ret);
  151. }
  152. EXPORT_SYMBOL_GPL(regmap_init);
  153. /**
  154. * regmap_exit(): Free a previously allocated register map
  155. */
  156. void regmap_exit(struct regmap *map)
  157. {
  158. kfree(map->work_buf);
  159. module_put(map->bus->owner);
  160. kfree(map);
  161. }
  162. EXPORT_SYMBOL_GPL(regmap_exit);
  163. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  164. const void *val, size_t val_len)
  165. {
  166. void *buf;
  167. int ret = -ENOTSUPP;
  168. size_t len;
  169. map->format.format_reg(map->work_buf, reg);
  170. /* Try to do a gather write if we can */
  171. if (map->bus->gather_write)
  172. ret = map->bus->gather_write(map->dev, map->work_buf,
  173. map->format.reg_bytes,
  174. val, val_len);
  175. /* Otherwise fall back on linearising by hand. */
  176. if (ret == -ENOTSUPP) {
  177. len = map->format.reg_bytes + val_len;
  178. buf = kmalloc(len, GFP_KERNEL);
  179. if (!buf)
  180. return -ENOMEM;
  181. memcpy(buf, map->work_buf, map->format.reg_bytes);
  182. memcpy(buf + map->format.reg_bytes, val, val_len);
  183. ret = map->bus->write(map->dev, buf, len);
  184. kfree(buf);
  185. }
  186. return ret;
  187. }
  188. static int _regmap_write(struct regmap *map, unsigned int reg,
  189. unsigned int val)
  190. {
  191. BUG_ON(!map->format.format_write && !map->format.format_val);
  192. if (map->format.format_write) {
  193. map->format.format_write(map, reg, val);
  194. return map->bus->write(map->dev, map->work_buf,
  195. map->format.buf_size);
  196. } else {
  197. map->format.format_val(map->work_buf + map->format.reg_bytes,
  198. val);
  199. return _regmap_raw_write(map, reg,
  200. map->work_buf + map->format.reg_bytes,
  201. map->format.val_bytes);
  202. }
  203. }
  204. /**
  205. * regmap_write(): Write a value to a single register
  206. *
  207. * @map: Register map to write to
  208. * @reg: Register to write to
  209. * @val: Value to be written
  210. *
  211. * A value of zero will be returned on success, a negative errno will
  212. * be returned in error cases.
  213. */
  214. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  215. {
  216. int ret;
  217. mutex_lock(&map->lock);
  218. ret = _regmap_write(map, reg, val);
  219. mutex_unlock(&map->lock);
  220. return ret;
  221. }
  222. EXPORT_SYMBOL_GPL(regmap_write);
  223. /**
  224. * regmap_raw_write(): Write raw values to one or more registers
  225. *
  226. * @map: Register map to write to
  227. * @reg: Initial register to write to
  228. * @val: Block of data to be written, laid out for direct transmission to the
  229. * device
  230. * @val_len: Length of data pointed to by val.
  231. *
  232. * This function is intended to be used for things like firmware
  233. * download where a large block of data needs to be transferred to the
  234. * device. No formatting will be done on the data provided.
  235. *
  236. * A value of zero will be returned on success, a negative errno will
  237. * be returned in error cases.
  238. */
  239. int regmap_raw_write(struct regmap *map, unsigned int reg,
  240. const void *val, size_t val_len)
  241. {
  242. int ret;
  243. mutex_lock(&map->lock);
  244. ret = _regmap_raw_write(map, reg, val, val_len);
  245. mutex_unlock(&map->lock);
  246. return ret;
  247. }
  248. EXPORT_SYMBOL_GPL(regmap_raw_write);
  249. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  250. unsigned int val_len)
  251. {
  252. u8 *u8 = map->work_buf;
  253. int ret;
  254. map->format.format_reg(map->work_buf, reg);
  255. /*
  256. * Some buses flag reads by setting the high bits in the
  257. * register addresss; since it's always the high bits for all
  258. * current formats we can do this here rather than in
  259. * formatting. This may break if we get interesting formats.
  260. */
  261. if (map->bus->read_flag_mask)
  262. u8[0] |= map->bus->read_flag_mask;
  263. ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
  264. val, map->format.val_bytes);
  265. if (ret != 0)
  266. return ret;
  267. return 0;
  268. }
  269. static int _regmap_read(struct regmap *map, unsigned int reg,
  270. unsigned int *val)
  271. {
  272. int ret;
  273. if (!map->format.parse_val)
  274. return -EINVAL;
  275. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  276. if (ret == 0)
  277. *val = map->format.parse_val(map->work_buf);
  278. return ret;
  279. }
  280. /**
  281. * regmap_read(): Read a value from a single register
  282. *
  283. * @map: Register map to write to
  284. * @reg: Register to be read from
  285. * @val: Pointer to store read value
  286. *
  287. * A value of zero will be returned on success, a negative errno will
  288. * be returned in error cases.
  289. */
  290. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  291. {
  292. int ret;
  293. mutex_lock(&map->lock);
  294. ret = _regmap_read(map, reg, val);
  295. mutex_unlock(&map->lock);
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(regmap_read);
  299. /**
  300. * regmap_raw_read(): Read raw data from the device
  301. *
  302. * @map: Register map to write to
  303. * @reg: First register to be read from
  304. * @val: Pointer to store read value
  305. * @val_len: Size of data to read
  306. *
  307. * A value of zero will be returned on success, a negative errno will
  308. * be returned in error cases.
  309. */
  310. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  311. size_t val_len)
  312. {
  313. int ret;
  314. mutex_lock(&map->lock);
  315. ret = _regmap_raw_read(map, reg, val, val_len);
  316. mutex_unlock(&map->lock);
  317. return ret;
  318. }
  319. EXPORT_SYMBOL_GPL(regmap_raw_read);
  320. /**
  321. * regmap_bulk_read(): Read multiple registers from the device
  322. *
  323. * @map: Register map to write to
  324. * @reg: First register to be read from
  325. * @val: Pointer to store read value, in native register size for device
  326. * @val_count: Number of registers to read
  327. *
  328. * A value of zero will be returned on success, a negative errno will
  329. * be returned in error cases.
  330. */
  331. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  332. size_t val_count)
  333. {
  334. int ret, i;
  335. size_t val_bytes = map->format.val_bytes;
  336. if (!map->format.parse_val)
  337. return -EINVAL;
  338. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  339. if (ret != 0)
  340. return ret;
  341. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  342. map->format.parse_val(val + i);
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  346. /**
  347. * remap_update_bits: Perform a read/modify/write cycle on the register map
  348. *
  349. * @map: Register map to update
  350. * @reg: Register to update
  351. * @mask: Bitmask to change
  352. * @val: New value for bitmask
  353. *
  354. * Returns zero for success, a negative number on error.
  355. */
  356. int regmap_update_bits(struct regmap *map, unsigned int reg,
  357. unsigned int mask, unsigned int val)
  358. {
  359. int ret;
  360. unsigned int tmp;
  361. mutex_lock(&map->lock);
  362. ret = _regmap_read(map, reg, &tmp);
  363. if (ret != 0)
  364. goto out;
  365. tmp &= ~mask;
  366. tmp |= val & mask;
  367. ret = _regmap_write(map, reg, tmp);
  368. out:
  369. mutex_unlock(&map->lock);
  370. return ret;
  371. }
  372. EXPORT_SYMBOL_GPL(regmap_update_bits);