core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /*
  2. * nvmem framework core.
  3. *
  4. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  5. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/export.h>
  18. #include <linux/fs.h>
  19. #include <linux/idr.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/nvmem-consumer.h>
  23. #include <linux/nvmem-provider.h>
  24. #include <linux/of.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. struct nvmem_device {
  28. const char *name;
  29. struct regmap *regmap;
  30. struct module *owner;
  31. struct device dev;
  32. int stride;
  33. int word_size;
  34. int ncells;
  35. int id;
  36. int users;
  37. size_t size;
  38. bool read_only;
  39. };
  40. struct nvmem_cell {
  41. const char *name;
  42. int offset;
  43. int bytes;
  44. int bit_offset;
  45. int nbits;
  46. struct nvmem_device *nvmem;
  47. struct list_head node;
  48. };
  49. static DEFINE_MUTEX(nvmem_mutex);
  50. static DEFINE_IDA(nvmem_ida);
  51. static LIST_HEAD(nvmem_cells);
  52. static DEFINE_MUTEX(nvmem_cells_mutex);
  53. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  54. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  55. struct bin_attribute *attr,
  56. char *buf, loff_t pos, size_t count)
  57. {
  58. struct device *dev = container_of(kobj, struct device, kobj);
  59. struct nvmem_device *nvmem = to_nvmem_device(dev);
  60. int rc;
  61. /* Stop the user from reading */
  62. if (pos >= nvmem->size)
  63. return 0;
  64. if (pos + count > nvmem->size)
  65. count = nvmem->size - pos;
  66. count = round_down(count, nvmem->word_size);
  67. rc = regmap_raw_read(nvmem->regmap, pos, buf, count);
  68. if (IS_ERR_VALUE(rc))
  69. return rc;
  70. return count;
  71. }
  72. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  73. struct bin_attribute *attr,
  74. char *buf, loff_t pos, size_t count)
  75. {
  76. struct device *dev = container_of(kobj, struct device, kobj);
  77. struct nvmem_device *nvmem = to_nvmem_device(dev);
  78. int rc;
  79. /* Stop the user from writing */
  80. if (pos >= nvmem->size)
  81. return 0;
  82. if (pos + count > nvmem->size)
  83. count = nvmem->size - pos;
  84. count = round_down(count, nvmem->word_size);
  85. rc = regmap_raw_write(nvmem->regmap, pos, buf, count);
  86. if (IS_ERR_VALUE(rc))
  87. return rc;
  88. return count;
  89. }
  90. /* default read/write permissions */
  91. static struct bin_attribute bin_attr_rw_nvmem = {
  92. .attr = {
  93. .name = "nvmem",
  94. .mode = S_IWUSR | S_IRUGO,
  95. },
  96. .read = bin_attr_nvmem_read,
  97. .write = bin_attr_nvmem_write,
  98. };
  99. static struct bin_attribute *nvmem_bin_rw_attributes[] = {
  100. &bin_attr_rw_nvmem,
  101. NULL,
  102. };
  103. static const struct attribute_group nvmem_bin_rw_group = {
  104. .bin_attrs = nvmem_bin_rw_attributes,
  105. };
  106. static const struct attribute_group *nvmem_rw_dev_groups[] = {
  107. &nvmem_bin_rw_group,
  108. NULL,
  109. };
  110. /* read only permission */
  111. static struct bin_attribute bin_attr_ro_nvmem = {
  112. .attr = {
  113. .name = "nvmem",
  114. .mode = S_IRUGO,
  115. },
  116. .read = bin_attr_nvmem_read,
  117. };
  118. static struct bin_attribute *nvmem_bin_ro_attributes[] = {
  119. &bin_attr_ro_nvmem,
  120. NULL,
  121. };
  122. static const struct attribute_group nvmem_bin_ro_group = {
  123. .bin_attrs = nvmem_bin_ro_attributes,
  124. };
  125. static const struct attribute_group *nvmem_ro_dev_groups[] = {
  126. &nvmem_bin_ro_group,
  127. NULL,
  128. };
  129. static void nvmem_release(struct device *dev)
  130. {
  131. struct nvmem_device *nvmem = to_nvmem_device(dev);
  132. ida_simple_remove(&nvmem_ida, nvmem->id);
  133. kfree(nvmem);
  134. }
  135. static const struct device_type nvmem_provider_type = {
  136. .release = nvmem_release,
  137. };
  138. static struct bus_type nvmem_bus_type = {
  139. .name = "nvmem",
  140. };
  141. static int of_nvmem_match(struct device *dev, void *nvmem_np)
  142. {
  143. return dev->of_node == nvmem_np;
  144. }
  145. static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
  146. {
  147. struct device *d;
  148. if (!nvmem_np)
  149. return NULL;
  150. d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
  151. if (!d)
  152. return NULL;
  153. return to_nvmem_device(d);
  154. }
  155. static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
  156. {
  157. struct nvmem_cell *p;
  158. list_for_each_entry(p, &nvmem_cells, node)
  159. if (p && !strcmp(p->name, cell_id))
  160. return p;
  161. return NULL;
  162. }
  163. static void nvmem_cell_drop(struct nvmem_cell *cell)
  164. {
  165. mutex_lock(&nvmem_cells_mutex);
  166. list_del(&cell->node);
  167. mutex_unlock(&nvmem_cells_mutex);
  168. kfree(cell);
  169. }
  170. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  171. {
  172. struct nvmem_cell *cell;
  173. struct list_head *p, *n;
  174. list_for_each_safe(p, n, &nvmem_cells) {
  175. cell = list_entry(p, struct nvmem_cell, node);
  176. if (cell->nvmem == nvmem)
  177. nvmem_cell_drop(cell);
  178. }
  179. }
  180. static void nvmem_cell_add(struct nvmem_cell *cell)
  181. {
  182. mutex_lock(&nvmem_cells_mutex);
  183. list_add_tail(&cell->node, &nvmem_cells);
  184. mutex_unlock(&nvmem_cells_mutex);
  185. }
  186. static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
  187. const struct nvmem_cell_info *info,
  188. struct nvmem_cell *cell)
  189. {
  190. cell->nvmem = nvmem;
  191. cell->offset = info->offset;
  192. cell->bytes = info->bytes;
  193. cell->name = info->name;
  194. cell->bit_offset = info->bit_offset;
  195. cell->nbits = info->nbits;
  196. if (cell->nbits)
  197. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  198. BITS_PER_BYTE);
  199. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  200. dev_err(&nvmem->dev,
  201. "cell %s unaligned to nvmem stride %d\n",
  202. cell->name, nvmem->stride);
  203. return -EINVAL;
  204. }
  205. return 0;
  206. }
  207. static int nvmem_add_cells(struct nvmem_device *nvmem,
  208. const struct nvmem_config *cfg)
  209. {
  210. struct nvmem_cell **cells;
  211. const struct nvmem_cell_info *info = cfg->cells;
  212. int i, rval;
  213. cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL);
  214. if (!cells)
  215. return -ENOMEM;
  216. for (i = 0; i < cfg->ncells; i++) {
  217. cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
  218. if (!cells[i]) {
  219. rval = -ENOMEM;
  220. goto err;
  221. }
  222. rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
  223. if (IS_ERR_VALUE(rval)) {
  224. kfree(cells[i]);
  225. goto err;
  226. }
  227. nvmem_cell_add(cells[i]);
  228. }
  229. nvmem->ncells = cfg->ncells;
  230. /* remove tmp array */
  231. kfree(cells);
  232. return 0;
  233. err:
  234. while (--i)
  235. nvmem_cell_drop(cells[i]);
  236. return rval;
  237. }
  238. /**
  239. * nvmem_register() - Register a nvmem device for given nvmem_config.
  240. * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  241. *
  242. * @config: nvmem device configuration with which nvmem device is created.
  243. *
  244. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  245. * on success.
  246. */
  247. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  248. {
  249. struct nvmem_device *nvmem;
  250. struct device_node *np;
  251. struct regmap *rm;
  252. int rval;
  253. if (!config->dev)
  254. return ERR_PTR(-EINVAL);
  255. rm = dev_get_regmap(config->dev, NULL);
  256. if (!rm) {
  257. dev_err(config->dev, "Regmap not found\n");
  258. return ERR_PTR(-EINVAL);
  259. }
  260. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  261. if (!nvmem)
  262. return ERR_PTR(-ENOMEM);
  263. rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
  264. if (rval < 0) {
  265. kfree(nvmem);
  266. return ERR_PTR(rval);
  267. }
  268. nvmem->id = rval;
  269. nvmem->regmap = rm;
  270. nvmem->owner = config->owner;
  271. nvmem->stride = regmap_get_reg_stride(rm);
  272. nvmem->word_size = regmap_get_val_bytes(rm);
  273. nvmem->size = regmap_get_max_register(rm) + nvmem->stride;
  274. nvmem->dev.type = &nvmem_provider_type;
  275. nvmem->dev.bus = &nvmem_bus_type;
  276. nvmem->dev.parent = config->dev;
  277. np = config->dev->of_node;
  278. nvmem->dev.of_node = np;
  279. dev_set_name(&nvmem->dev, "%s%d",
  280. config->name ? : "nvmem", config->id);
  281. nvmem->read_only = of_property_read_bool(np, "read-only") |
  282. config->read_only;
  283. nvmem->dev.groups = nvmem->read_only ? nvmem_ro_dev_groups :
  284. nvmem_rw_dev_groups;
  285. device_initialize(&nvmem->dev);
  286. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  287. rval = device_add(&nvmem->dev);
  288. if (rval) {
  289. ida_simple_remove(&nvmem_ida, nvmem->id);
  290. kfree(nvmem);
  291. return ERR_PTR(rval);
  292. }
  293. if (config->cells)
  294. nvmem_add_cells(nvmem, config);
  295. return nvmem;
  296. }
  297. EXPORT_SYMBOL_GPL(nvmem_register);
  298. /**
  299. * nvmem_unregister() - Unregister previously registered nvmem device
  300. *
  301. * @nvmem: Pointer to previously registered nvmem device.
  302. *
  303. * Return: Will be an negative on error or a zero on success.
  304. */
  305. int nvmem_unregister(struct nvmem_device *nvmem)
  306. {
  307. mutex_lock(&nvmem_mutex);
  308. if (nvmem->users) {
  309. mutex_unlock(&nvmem_mutex);
  310. return -EBUSY;
  311. }
  312. mutex_unlock(&nvmem_mutex);
  313. nvmem_device_remove_all_cells(nvmem);
  314. device_del(&nvmem->dev);
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(nvmem_unregister);
  318. static struct nvmem_device *__nvmem_device_get(struct device_node *np,
  319. struct nvmem_cell **cellp,
  320. const char *cell_id)
  321. {
  322. struct nvmem_device *nvmem = NULL;
  323. mutex_lock(&nvmem_mutex);
  324. if (np) {
  325. nvmem = of_nvmem_find(np);
  326. if (!nvmem) {
  327. mutex_unlock(&nvmem_mutex);
  328. return ERR_PTR(-EPROBE_DEFER);
  329. }
  330. } else {
  331. struct nvmem_cell *cell = nvmem_find_cell(cell_id);
  332. if (cell) {
  333. nvmem = cell->nvmem;
  334. *cellp = cell;
  335. }
  336. if (!nvmem) {
  337. mutex_unlock(&nvmem_mutex);
  338. return ERR_PTR(-ENOENT);
  339. }
  340. }
  341. nvmem->users++;
  342. mutex_unlock(&nvmem_mutex);
  343. if (!try_module_get(nvmem->owner)) {
  344. dev_err(&nvmem->dev,
  345. "could not increase module refcount for cell %s\n",
  346. nvmem->name);
  347. mutex_lock(&nvmem_mutex);
  348. nvmem->users--;
  349. mutex_unlock(&nvmem_mutex);
  350. return ERR_PTR(-EINVAL);
  351. }
  352. return nvmem;
  353. }
  354. static void __nvmem_device_put(struct nvmem_device *nvmem)
  355. {
  356. module_put(nvmem->owner);
  357. mutex_lock(&nvmem_mutex);
  358. nvmem->users--;
  359. mutex_unlock(&nvmem_mutex);
  360. }
  361. static int nvmem_match(struct device *dev, void *data)
  362. {
  363. return !strcmp(dev_name(dev), data);
  364. }
  365. static struct nvmem_device *nvmem_find(const char *name)
  366. {
  367. struct device *d;
  368. d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match);
  369. if (!d)
  370. return NULL;
  371. return to_nvmem_device(d);
  372. }
  373. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  374. /**
  375. * of_nvmem_device_get() - Get nvmem device from a given id
  376. *
  377. * @dev node: Device tree node that uses the nvmem device
  378. * @id: nvmem name from nvmem-names property.
  379. *
  380. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  381. * on success.
  382. */
  383. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  384. {
  385. struct device_node *nvmem_np;
  386. int index;
  387. index = of_property_match_string(np, "nvmem-names", id);
  388. nvmem_np = of_parse_phandle(np, "nvmem", index);
  389. if (!nvmem_np)
  390. return ERR_PTR(-EINVAL);
  391. return __nvmem_device_get(nvmem_np, NULL, NULL);
  392. }
  393. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  394. #endif
  395. /**
  396. * nvmem_device_get() - Get nvmem device from a given id
  397. *
  398. * @dev : Device that uses the nvmem device
  399. * @id: nvmem name from nvmem-names property.
  400. *
  401. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  402. * on success.
  403. */
  404. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  405. {
  406. if (dev->of_node) { /* try dt first */
  407. struct nvmem_device *nvmem;
  408. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  409. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  410. return nvmem;
  411. }
  412. return nvmem_find(dev_name);
  413. }
  414. EXPORT_SYMBOL_GPL(nvmem_device_get);
  415. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  416. {
  417. struct nvmem_device **nvmem = res;
  418. if (WARN_ON(!nvmem || !*nvmem))
  419. return 0;
  420. return *nvmem == data;
  421. }
  422. static void devm_nvmem_device_release(struct device *dev, void *res)
  423. {
  424. nvmem_device_put(*(struct nvmem_device **)res);
  425. }
  426. /**
  427. * devm_nvmem_device_put() - put alredy got nvmem device
  428. *
  429. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  430. * that needs to be released.
  431. */
  432. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  433. {
  434. int ret;
  435. ret = devres_release(dev, devm_nvmem_device_release,
  436. devm_nvmem_device_match, nvmem);
  437. WARN_ON(ret);
  438. }
  439. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  440. /**
  441. * nvmem_device_put() - put alredy got nvmem device
  442. *
  443. * @nvmem: pointer to nvmem device that needs to be released.
  444. */
  445. void nvmem_device_put(struct nvmem_device *nvmem)
  446. {
  447. __nvmem_device_put(nvmem);
  448. }
  449. EXPORT_SYMBOL_GPL(nvmem_device_put);
  450. /**
  451. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  452. *
  453. * @dev node: Device tree node that uses the nvmem cell
  454. * @id: nvmem name in nvmems property.
  455. *
  456. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  457. * on success. The nvmem_cell will be freed by the automatically once the
  458. * device is freed.
  459. */
  460. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  461. {
  462. struct nvmem_device **ptr, *nvmem;
  463. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  464. if (!ptr)
  465. return ERR_PTR(-ENOMEM);
  466. nvmem = nvmem_device_get(dev, id);
  467. if (!IS_ERR(nvmem)) {
  468. *ptr = nvmem;
  469. devres_add(dev, ptr);
  470. } else {
  471. devres_free(ptr);
  472. }
  473. return nvmem;
  474. }
  475. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  476. static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
  477. {
  478. struct nvmem_cell *cell = NULL;
  479. struct nvmem_device *nvmem;
  480. nvmem = __nvmem_device_get(NULL, &cell, cell_id);
  481. if (IS_ERR(nvmem))
  482. return ERR_CAST(nvmem);
  483. return cell;
  484. }
  485. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  486. /**
  487. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  488. *
  489. * @dev node: Device tree node that uses the nvmem cell
  490. * @id: nvmem cell name from nvmem-cell-names property.
  491. *
  492. * Return: Will be an ERR_PTR() on error or a valid pointer
  493. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  494. * nvmem_cell_put().
  495. */
  496. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
  497. const char *name)
  498. {
  499. struct device_node *cell_np, *nvmem_np;
  500. struct nvmem_cell *cell;
  501. struct nvmem_device *nvmem;
  502. const __be32 *addr;
  503. int rval, len, index;
  504. index = of_property_match_string(np, "nvmem-cell-names", name);
  505. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  506. if (!cell_np)
  507. return ERR_PTR(-EINVAL);
  508. nvmem_np = of_get_next_parent(cell_np);
  509. if (!nvmem_np)
  510. return ERR_PTR(-EINVAL);
  511. nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
  512. if (IS_ERR(nvmem))
  513. return ERR_CAST(nvmem);
  514. addr = of_get_property(cell_np, "reg", &len);
  515. if (!addr || (len < 2 * sizeof(u32))) {
  516. dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n",
  517. cell_np->full_name);
  518. rval = -EINVAL;
  519. goto err_mem;
  520. }
  521. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  522. if (!cell) {
  523. rval = -ENOMEM;
  524. goto err_mem;
  525. }
  526. cell->nvmem = nvmem;
  527. cell->offset = be32_to_cpup(addr++);
  528. cell->bytes = be32_to_cpup(addr);
  529. cell->name = cell_np->name;
  530. addr = of_get_property(cell_np, "bits", &len);
  531. if (addr && len == (2 * sizeof(u32))) {
  532. cell->bit_offset = be32_to_cpup(addr++);
  533. cell->nbits = be32_to_cpup(addr);
  534. }
  535. if (cell->nbits)
  536. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  537. BITS_PER_BYTE);
  538. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  539. dev_err(&nvmem->dev,
  540. "cell %s unaligned to nvmem stride %d\n",
  541. cell->name, nvmem->stride);
  542. rval = -EINVAL;
  543. goto err_sanity;
  544. }
  545. nvmem_cell_add(cell);
  546. return cell;
  547. err_sanity:
  548. kfree(cell);
  549. err_mem:
  550. __nvmem_device_put(nvmem);
  551. return ERR_PTR(rval);
  552. }
  553. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  554. #endif
  555. /**
  556. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  557. *
  558. * @dev node: Device tree node that uses the nvmem cell
  559. * @id: nvmem cell name to get.
  560. *
  561. * Return: Will be an ERR_PTR() on error or a valid pointer
  562. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  563. * nvmem_cell_put().
  564. */
  565. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
  566. {
  567. struct nvmem_cell *cell;
  568. if (dev->of_node) { /* try dt first */
  569. cell = of_nvmem_cell_get(dev->of_node, cell_id);
  570. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  571. return cell;
  572. }
  573. return nvmem_cell_get_from_list(cell_id);
  574. }
  575. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  576. static void devm_nvmem_cell_release(struct device *dev, void *res)
  577. {
  578. nvmem_cell_put(*(struct nvmem_cell **)res);
  579. }
  580. /**
  581. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  582. *
  583. * @dev node: Device tree node that uses the nvmem cell
  584. * @id: nvmem id in nvmem-names property.
  585. *
  586. * Return: Will be an ERR_PTR() on error or a valid pointer
  587. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  588. * automatically once the device is freed.
  589. */
  590. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  591. {
  592. struct nvmem_cell **ptr, *cell;
  593. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  594. if (!ptr)
  595. return ERR_PTR(-ENOMEM);
  596. cell = nvmem_cell_get(dev, id);
  597. if (!IS_ERR(cell)) {
  598. *ptr = cell;
  599. devres_add(dev, ptr);
  600. } else {
  601. devres_free(ptr);
  602. }
  603. return cell;
  604. }
  605. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  606. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  607. {
  608. struct nvmem_cell **c = res;
  609. if (WARN_ON(!c || !*c))
  610. return 0;
  611. return *c == data;
  612. }
  613. /**
  614. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  615. * from devm_nvmem_cell_get.
  616. *
  617. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get()
  618. */
  619. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  620. {
  621. int ret;
  622. ret = devres_release(dev, devm_nvmem_cell_release,
  623. devm_nvmem_cell_match, cell);
  624. WARN_ON(ret);
  625. }
  626. EXPORT_SYMBOL(devm_nvmem_cell_put);
  627. /**
  628. * nvmem_cell_put() - Release previously allocated nvmem cell.
  629. *
  630. * @cell: Previously allocated nvmem cell by nvmem_cell_get()
  631. */
  632. void nvmem_cell_put(struct nvmem_cell *cell)
  633. {
  634. struct nvmem_device *nvmem = cell->nvmem;
  635. __nvmem_device_put(nvmem);
  636. nvmem_cell_drop(cell);
  637. }
  638. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  639. static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell,
  640. void *buf)
  641. {
  642. u8 *p, *b;
  643. int i, bit_offset = cell->bit_offset;
  644. p = b = buf;
  645. if (bit_offset) {
  646. /* First shift */
  647. *b++ >>= bit_offset;
  648. /* setup rest of the bytes if any */
  649. for (i = 1; i < cell->bytes; i++) {
  650. /* Get bits from next byte and shift them towards msb */
  651. *p |= *b << (BITS_PER_BYTE - bit_offset);
  652. p = b;
  653. *b++ >>= bit_offset;
  654. }
  655. /* result fits in less bytes */
  656. if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
  657. *p-- = 0;
  658. }
  659. /* clear msb bits if any leftover in the last byte */
  660. *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
  661. }
  662. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  663. struct nvmem_cell *cell,
  664. void *buf, size_t *len)
  665. {
  666. int rc;
  667. rc = regmap_raw_read(nvmem->regmap, cell->offset, buf, cell->bytes);
  668. if (IS_ERR_VALUE(rc))
  669. return rc;
  670. /* shift bits in-place */
  671. if (cell->bit_offset || cell->nbits)
  672. nvmem_shift_read_buffer_in_place(cell, buf);
  673. *len = cell->bytes;
  674. return 0;
  675. }
  676. /**
  677. * nvmem_cell_read() - Read a given nvmem cell
  678. *
  679. * @cell: nvmem cell to be read.
  680. * @len: pointer to length of cell which will be populated on successful read.
  681. *
  682. * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success.
  683. * The buffer should be freed by the consumer with a kfree().
  684. */
  685. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  686. {
  687. struct nvmem_device *nvmem = cell->nvmem;
  688. u8 *buf;
  689. int rc;
  690. if (!nvmem || !nvmem->regmap)
  691. return ERR_PTR(-EINVAL);
  692. buf = kzalloc(cell->bytes, GFP_KERNEL);
  693. if (!buf)
  694. return ERR_PTR(-ENOMEM);
  695. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  696. if (IS_ERR_VALUE(rc)) {
  697. kfree(buf);
  698. return ERR_PTR(rc);
  699. }
  700. return buf;
  701. }
  702. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  703. static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  704. u8 *_buf, int len)
  705. {
  706. struct nvmem_device *nvmem = cell->nvmem;
  707. int i, rc, nbits, bit_offset = cell->bit_offset;
  708. u8 v, *p, *buf, *b, pbyte, pbits;
  709. nbits = cell->nbits;
  710. buf = kzalloc(cell->bytes, GFP_KERNEL);
  711. if (!buf)
  712. return ERR_PTR(-ENOMEM);
  713. memcpy(buf, _buf, len);
  714. p = b = buf;
  715. if (bit_offset) {
  716. pbyte = *b;
  717. *b <<= bit_offset;
  718. /* setup the first byte with lsb bits from nvmem */
  719. rc = regmap_raw_read(nvmem->regmap, cell->offset, &v, 1);
  720. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  721. /* setup rest of the byte if any */
  722. for (i = 1; i < cell->bytes; i++) {
  723. /* Get last byte bits and shift them towards lsb */
  724. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  725. pbyte = *b;
  726. p = b;
  727. *b <<= bit_offset;
  728. *b++ |= pbits;
  729. }
  730. }
  731. /* if it's not end on byte boundary */
  732. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  733. /* setup the last byte with msb bits from nvmem */
  734. rc = regmap_raw_read(nvmem->regmap,
  735. cell->offset + cell->bytes - 1, &v, 1);
  736. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  737. }
  738. return buf;
  739. }
  740. /**
  741. * nvmem_cell_write() - Write to a given nvmem cell
  742. *
  743. * @cell: nvmem cell to be written.
  744. * @buf: Buffer to be written.
  745. * @len: length of buffer to be written to nvmem cell.
  746. *
  747. * Return: length of bytes written or negative on failure.
  748. */
  749. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  750. {
  751. struct nvmem_device *nvmem = cell->nvmem;
  752. int rc;
  753. if (!nvmem || !nvmem->regmap || nvmem->read_only ||
  754. (cell->bit_offset == 0 && len != cell->bytes))
  755. return -EINVAL;
  756. if (cell->bit_offset || cell->nbits) {
  757. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  758. if (IS_ERR(buf))
  759. return PTR_ERR(buf);
  760. }
  761. rc = regmap_raw_write(nvmem->regmap, cell->offset, buf, cell->bytes);
  762. /* free the tmp buffer */
  763. if (cell->bit_offset || cell->nbits)
  764. kfree(buf);
  765. if (IS_ERR_VALUE(rc))
  766. return rc;
  767. return len;
  768. }
  769. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  770. /**
  771. * nvmem_device_cell_read() - Read a given nvmem device and cell
  772. *
  773. * @nvmem: nvmem device to read from.
  774. * @info: nvmem cell info to be read.
  775. * @buf: buffer pointer which will be populated on successful read.
  776. *
  777. * Return: length of successful bytes read on success and negative
  778. * error code on error.
  779. */
  780. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  781. struct nvmem_cell_info *info, void *buf)
  782. {
  783. struct nvmem_cell cell;
  784. int rc;
  785. ssize_t len;
  786. if (!nvmem || !nvmem->regmap)
  787. return -EINVAL;
  788. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  789. if (IS_ERR_VALUE(rc))
  790. return rc;
  791. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  792. if (IS_ERR_VALUE(rc))
  793. return rc;
  794. return len;
  795. }
  796. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  797. /**
  798. * nvmem_device_cell_write() - Write cell to a given nvmem device
  799. *
  800. * @nvmem: nvmem device to be written to.
  801. * @info: nvmem cell info to be written
  802. * @buf: buffer to be written to cell.
  803. *
  804. * Return: length of bytes written or negative error code on failure.
  805. * */
  806. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  807. struct nvmem_cell_info *info, void *buf)
  808. {
  809. struct nvmem_cell cell;
  810. int rc;
  811. if (!nvmem || !nvmem->regmap)
  812. return -EINVAL;
  813. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  814. if (IS_ERR_VALUE(rc))
  815. return rc;
  816. return nvmem_cell_write(&cell, buf, cell.bytes);
  817. }
  818. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  819. /**
  820. * nvmem_device_read() - Read from a given nvmem device
  821. *
  822. * @nvmem: nvmem device to read from.
  823. * @offset: offset in nvmem device.
  824. * @bytes: number of bytes to read.
  825. * @buf: buffer pointer which will be populated on successful read.
  826. *
  827. * Return: length of successful bytes read on success and negative
  828. * error code on error.
  829. */
  830. int nvmem_device_read(struct nvmem_device *nvmem,
  831. unsigned int offset,
  832. size_t bytes, void *buf)
  833. {
  834. int rc;
  835. if (!nvmem || !nvmem->regmap)
  836. return -EINVAL;
  837. rc = regmap_raw_read(nvmem->regmap, offset, buf, bytes);
  838. if (IS_ERR_VALUE(rc))
  839. return rc;
  840. return bytes;
  841. }
  842. EXPORT_SYMBOL_GPL(nvmem_device_read);
  843. /**
  844. * nvmem_device_write() - Write cell to a given nvmem device
  845. *
  846. * @nvmem: nvmem device to be written to.
  847. * @offset: offset in nvmem device.
  848. * @bytes: number of bytes to write.
  849. * @buf: buffer to be written.
  850. *
  851. * Return: length of bytes written or negative error code on failure.
  852. * */
  853. int nvmem_device_write(struct nvmem_device *nvmem,
  854. unsigned int offset,
  855. size_t bytes, void *buf)
  856. {
  857. int rc;
  858. if (!nvmem || !nvmem->regmap)
  859. return -EINVAL;
  860. rc = regmap_raw_write(nvmem->regmap, offset, buf, bytes);
  861. if (IS_ERR_VALUE(rc))
  862. return rc;
  863. return bytes;
  864. }
  865. EXPORT_SYMBOL_GPL(nvmem_device_write);
  866. static int __init nvmem_init(void)
  867. {
  868. return bus_register(&nvmem_bus_type);
  869. }
  870. static void __exit nvmem_exit(void)
  871. {
  872. bus_unregister(&nvmem_bus_type);
  873. }
  874. subsys_initcall(nvmem_init);
  875. module_exit(nvmem_exit);
  876. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  877. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  878. MODULE_DESCRIPTION("nvmem Driver Core");
  879. MODULE_LICENSE("GPL v2");