core.c 27 KB

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