core.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. * @np: 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. * @dev_name: name of the requested nvmem device.
  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. * @dev: Device that uses the nvmem device.
  547. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  548. * that needs to be released.
  549. */
  550. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  551. {
  552. int ret;
  553. ret = devres_release(dev, devm_nvmem_device_release,
  554. devm_nvmem_device_match, nvmem);
  555. WARN_ON(ret);
  556. }
  557. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  558. /**
  559. * nvmem_device_put() - put alredy got nvmem device
  560. *
  561. * @nvmem: pointer to nvmem device that needs to be released.
  562. */
  563. void nvmem_device_put(struct nvmem_device *nvmem)
  564. {
  565. __nvmem_device_put(nvmem);
  566. }
  567. EXPORT_SYMBOL_GPL(nvmem_device_put);
  568. /**
  569. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  570. *
  571. * @dev: Device that requests the nvmem device.
  572. * @id: name id for the requested nvmem device.
  573. *
  574. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  575. * on success. The nvmem_cell will be freed by the automatically once the
  576. * device is freed.
  577. */
  578. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  579. {
  580. struct nvmem_device **ptr, *nvmem;
  581. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  582. if (!ptr)
  583. return ERR_PTR(-ENOMEM);
  584. nvmem = nvmem_device_get(dev, id);
  585. if (!IS_ERR(nvmem)) {
  586. *ptr = nvmem;
  587. devres_add(dev, ptr);
  588. } else {
  589. devres_free(ptr);
  590. }
  591. return nvmem;
  592. }
  593. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  594. static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
  595. {
  596. struct nvmem_cell *cell = NULL;
  597. struct nvmem_device *nvmem;
  598. nvmem = __nvmem_device_get(NULL, &cell, cell_id);
  599. if (IS_ERR(nvmem))
  600. return ERR_CAST(nvmem);
  601. return cell;
  602. }
  603. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  604. /**
  605. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  606. *
  607. * @np: Device tree node that uses the nvmem cell.
  608. * @name: nvmem cell name from nvmem-cell-names property, or NULL
  609. * for the cell at index 0 (the lone cell with no accompanying
  610. * nvmem-cell-names property).
  611. *
  612. * Return: Will be an ERR_PTR() on error or a valid pointer
  613. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  614. * nvmem_cell_put().
  615. */
  616. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
  617. const char *name)
  618. {
  619. struct device_node *cell_np, *nvmem_np;
  620. struct nvmem_cell *cell;
  621. struct nvmem_device *nvmem;
  622. const __be32 *addr;
  623. int rval, len;
  624. int index = 0;
  625. /* if cell name exists, find index to the name */
  626. if (name)
  627. index = of_property_match_string(np, "nvmem-cell-names", name);
  628. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  629. if (!cell_np)
  630. return ERR_PTR(-EINVAL);
  631. nvmem_np = of_get_next_parent(cell_np);
  632. if (!nvmem_np)
  633. return ERR_PTR(-EINVAL);
  634. nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
  635. if (IS_ERR(nvmem))
  636. return ERR_CAST(nvmem);
  637. addr = of_get_property(cell_np, "reg", &len);
  638. if (!addr || (len < 2 * sizeof(u32))) {
  639. dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n",
  640. cell_np->full_name);
  641. rval = -EINVAL;
  642. goto err_mem;
  643. }
  644. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  645. if (!cell) {
  646. rval = -ENOMEM;
  647. goto err_mem;
  648. }
  649. cell->nvmem = nvmem;
  650. cell->offset = be32_to_cpup(addr++);
  651. cell->bytes = be32_to_cpup(addr);
  652. cell->name = cell_np->name;
  653. addr = of_get_property(cell_np, "bits", &len);
  654. if (addr && len == (2 * sizeof(u32))) {
  655. cell->bit_offset = be32_to_cpup(addr++);
  656. cell->nbits = be32_to_cpup(addr);
  657. }
  658. if (cell->nbits)
  659. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  660. BITS_PER_BYTE);
  661. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  662. dev_err(&nvmem->dev,
  663. "cell %s unaligned to nvmem stride %d\n",
  664. cell->name, nvmem->stride);
  665. rval = -EINVAL;
  666. goto err_sanity;
  667. }
  668. nvmem_cell_add(cell);
  669. return cell;
  670. err_sanity:
  671. kfree(cell);
  672. err_mem:
  673. __nvmem_device_put(nvmem);
  674. return ERR_PTR(rval);
  675. }
  676. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  677. #endif
  678. /**
  679. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  680. *
  681. * @dev: Device that requests the nvmem cell.
  682. * @cell_id: nvmem cell name to get.
  683. *
  684. * Return: Will be an ERR_PTR() on error or a valid pointer
  685. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  686. * nvmem_cell_put().
  687. */
  688. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
  689. {
  690. struct nvmem_cell *cell;
  691. if (dev->of_node) { /* try dt first */
  692. cell = of_nvmem_cell_get(dev->of_node, cell_id);
  693. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  694. return cell;
  695. }
  696. return nvmem_cell_get_from_list(cell_id);
  697. }
  698. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  699. static void devm_nvmem_cell_release(struct device *dev, void *res)
  700. {
  701. nvmem_cell_put(*(struct nvmem_cell **)res);
  702. }
  703. /**
  704. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  705. *
  706. * @dev: Device that requests the nvmem cell.
  707. * @id: nvmem cell name id to get.
  708. *
  709. * Return: Will be an ERR_PTR() on error or a valid pointer
  710. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  711. * automatically once the device is freed.
  712. */
  713. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  714. {
  715. struct nvmem_cell **ptr, *cell;
  716. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  717. if (!ptr)
  718. return ERR_PTR(-ENOMEM);
  719. cell = nvmem_cell_get(dev, id);
  720. if (!IS_ERR(cell)) {
  721. *ptr = cell;
  722. devres_add(dev, ptr);
  723. } else {
  724. devres_free(ptr);
  725. }
  726. return cell;
  727. }
  728. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  729. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  730. {
  731. struct nvmem_cell **c = res;
  732. if (WARN_ON(!c || !*c))
  733. return 0;
  734. return *c == data;
  735. }
  736. /**
  737. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  738. * from devm_nvmem_cell_get.
  739. *
  740. * @dev: Device that requests the nvmem cell.
  741. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  742. */
  743. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  744. {
  745. int ret;
  746. ret = devres_release(dev, devm_nvmem_cell_release,
  747. devm_nvmem_cell_match, cell);
  748. WARN_ON(ret);
  749. }
  750. EXPORT_SYMBOL(devm_nvmem_cell_put);
  751. /**
  752. * nvmem_cell_put() - Release previously allocated nvmem cell.
  753. *
  754. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  755. */
  756. void nvmem_cell_put(struct nvmem_cell *cell)
  757. {
  758. struct nvmem_device *nvmem = cell->nvmem;
  759. __nvmem_device_put(nvmem);
  760. nvmem_cell_drop(cell);
  761. }
  762. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  763. static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell,
  764. void *buf)
  765. {
  766. u8 *p, *b;
  767. int i, bit_offset = cell->bit_offset;
  768. p = b = buf;
  769. if (bit_offset) {
  770. /* First shift */
  771. *b++ >>= bit_offset;
  772. /* setup rest of the bytes if any */
  773. for (i = 1; i < cell->bytes; i++) {
  774. /* Get bits from next byte and shift them towards msb */
  775. *p |= *b << (BITS_PER_BYTE - bit_offset);
  776. p = b;
  777. *b++ >>= bit_offset;
  778. }
  779. /* result fits in less bytes */
  780. if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
  781. *p-- = 0;
  782. }
  783. /* clear msb bits if any leftover in the last byte */
  784. *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
  785. }
  786. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  787. struct nvmem_cell *cell,
  788. void *buf, size_t *len)
  789. {
  790. int rc;
  791. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  792. if (rc)
  793. return rc;
  794. /* shift bits in-place */
  795. if (cell->bit_offset || cell->nbits)
  796. nvmem_shift_read_buffer_in_place(cell, buf);
  797. if (len)
  798. *len = cell->bytes;
  799. return 0;
  800. }
  801. /**
  802. * nvmem_cell_read() - Read a given nvmem cell
  803. *
  804. * @cell: nvmem cell to be read.
  805. * @len: pointer to length of cell which will be populated on successful read;
  806. * can be NULL.
  807. *
  808. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  809. * buffer should be freed by the consumer with a kfree().
  810. */
  811. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  812. {
  813. struct nvmem_device *nvmem = cell->nvmem;
  814. u8 *buf;
  815. int rc;
  816. if (!nvmem)
  817. return ERR_PTR(-EINVAL);
  818. buf = kzalloc(cell->bytes, GFP_KERNEL);
  819. if (!buf)
  820. return ERR_PTR(-ENOMEM);
  821. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  822. if (rc) {
  823. kfree(buf);
  824. return ERR_PTR(rc);
  825. }
  826. return buf;
  827. }
  828. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  829. static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  830. u8 *_buf, int len)
  831. {
  832. struct nvmem_device *nvmem = cell->nvmem;
  833. int i, rc, nbits, bit_offset = cell->bit_offset;
  834. u8 v, *p, *buf, *b, pbyte, pbits;
  835. nbits = cell->nbits;
  836. buf = kzalloc(cell->bytes, GFP_KERNEL);
  837. if (!buf)
  838. return ERR_PTR(-ENOMEM);
  839. memcpy(buf, _buf, len);
  840. p = b = buf;
  841. if (bit_offset) {
  842. pbyte = *b;
  843. *b <<= bit_offset;
  844. /* setup the first byte with lsb bits from nvmem */
  845. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  846. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  847. /* setup rest of the byte if any */
  848. for (i = 1; i < cell->bytes; i++) {
  849. /* Get last byte bits and shift them towards lsb */
  850. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  851. pbyte = *b;
  852. p = b;
  853. *b <<= bit_offset;
  854. *b++ |= pbits;
  855. }
  856. }
  857. /* if it's not end on byte boundary */
  858. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  859. /* setup the last byte with msb bits from nvmem */
  860. rc = nvmem_reg_read(nvmem,
  861. cell->offset + cell->bytes - 1, &v, 1);
  862. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  863. }
  864. return buf;
  865. }
  866. /**
  867. * nvmem_cell_write() - Write to a given nvmem cell
  868. *
  869. * @cell: nvmem cell to be written.
  870. * @buf: Buffer to be written.
  871. * @len: length of buffer to be written to nvmem cell.
  872. *
  873. * Return: length of bytes written or negative on failure.
  874. */
  875. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  876. {
  877. struct nvmem_device *nvmem = cell->nvmem;
  878. int rc;
  879. if (!nvmem || nvmem->read_only ||
  880. (cell->bit_offset == 0 && len != cell->bytes))
  881. return -EINVAL;
  882. if (cell->bit_offset || cell->nbits) {
  883. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  884. if (IS_ERR(buf))
  885. return PTR_ERR(buf);
  886. }
  887. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  888. /* free the tmp buffer */
  889. if (cell->bit_offset || cell->nbits)
  890. kfree(buf);
  891. if (rc)
  892. return rc;
  893. return len;
  894. }
  895. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  896. /**
  897. * nvmem_device_cell_read() - Read a given nvmem device and cell
  898. *
  899. * @nvmem: nvmem device to read from.
  900. * @info: nvmem cell info to be read.
  901. * @buf: buffer pointer which will be populated on successful read.
  902. *
  903. * Return: length of successful bytes read on success and negative
  904. * error code on error.
  905. */
  906. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  907. struct nvmem_cell_info *info, void *buf)
  908. {
  909. struct nvmem_cell cell;
  910. int rc;
  911. ssize_t len;
  912. if (!nvmem)
  913. return -EINVAL;
  914. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  915. if (rc)
  916. return rc;
  917. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  918. if (rc)
  919. return rc;
  920. return len;
  921. }
  922. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  923. /**
  924. * nvmem_device_cell_write() - Write cell to a given nvmem device
  925. *
  926. * @nvmem: nvmem device to be written to.
  927. * @info: nvmem cell info to be written.
  928. * @buf: buffer to be written to cell.
  929. *
  930. * Return: length of bytes written or negative error code on failure.
  931. * */
  932. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  933. struct nvmem_cell_info *info, void *buf)
  934. {
  935. struct nvmem_cell cell;
  936. int rc;
  937. if (!nvmem)
  938. return -EINVAL;
  939. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  940. if (rc)
  941. return rc;
  942. return nvmem_cell_write(&cell, buf, cell.bytes);
  943. }
  944. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  945. /**
  946. * nvmem_device_read() - Read from a given nvmem device
  947. *
  948. * @nvmem: nvmem device to read from.
  949. * @offset: offset in nvmem device.
  950. * @bytes: number of bytes to read.
  951. * @buf: buffer pointer which will be populated on successful read.
  952. *
  953. * Return: length of successful bytes read on success and negative
  954. * error code on error.
  955. */
  956. int nvmem_device_read(struct nvmem_device *nvmem,
  957. unsigned int offset,
  958. size_t bytes, void *buf)
  959. {
  960. int rc;
  961. if (!nvmem)
  962. return -EINVAL;
  963. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  964. if (rc)
  965. return rc;
  966. return bytes;
  967. }
  968. EXPORT_SYMBOL_GPL(nvmem_device_read);
  969. /**
  970. * nvmem_device_write() - Write cell to a given nvmem device
  971. *
  972. * @nvmem: nvmem device to be written to.
  973. * @offset: offset in nvmem device.
  974. * @bytes: number of bytes to write.
  975. * @buf: buffer to be written.
  976. *
  977. * Return: length of bytes written or negative error code on failure.
  978. * */
  979. int nvmem_device_write(struct nvmem_device *nvmem,
  980. unsigned int offset,
  981. size_t bytes, void *buf)
  982. {
  983. int rc;
  984. if (!nvmem)
  985. return -EINVAL;
  986. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  987. if (rc)
  988. return rc;
  989. return bytes;
  990. }
  991. EXPORT_SYMBOL_GPL(nvmem_device_write);
  992. static int __init nvmem_init(void)
  993. {
  994. return bus_register(&nvmem_bus_type);
  995. }
  996. static void __exit nvmem_exit(void)
  997. {
  998. bus_unregister(&nvmem_bus_type);
  999. }
  1000. subsys_initcall(nvmem_init);
  1001. module_exit(nvmem_exit);
  1002. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1003. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1004. MODULE_DESCRIPTION("nvmem Driver Core");
  1005. MODULE_LICENSE("GPL v2");