Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 16 | #include <linux/mod_devicetable.h> |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 18 | #include <linux/nvmem-provider.h> |
| 19 | #include <linux/platform_device.h> |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 20 | |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 21 | struct qfprom_priv { |
| 22 | void __iomem *base; |
| 23 | }; |
| 24 | |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 25 | static int qfprom_reg_read(void *context, |
| 26 | unsigned int reg, void *_val, size_t bytes) |
| 27 | { |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 28 | struct qfprom_priv *priv = context; |
Vivek Gautam | 01d0d2c | 2017-01-04 16:18:09 +0000 | [diff] [blame] | 29 | u8 *val = _val; |
| 30 | int i = 0, words = bytes; |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 31 | |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 32 | while (words--) |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 33 | *val++ = readb(priv->base + reg + i++); |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int qfprom_reg_write(void *context, |
| 39 | unsigned int reg, void *_val, size_t bytes) |
| 40 | { |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 41 | struct qfprom_priv *priv = context; |
Vivek Gautam | 01d0d2c | 2017-01-04 16:18:09 +0000 | [diff] [blame] | 42 | u8 *val = _val; |
| 43 | int i = 0, words = bytes; |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 44 | |
| 45 | while (words--) |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 46 | writeb(*val++, priv->base + reg + i++); |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 50 | |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 51 | static struct nvmem_config econfig = { |
| 52 | .name = "qfprom", |
Vivek Gautam | 01d0d2c | 2017-01-04 16:18:09 +0000 | [diff] [blame] | 53 | .stride = 1, |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 54 | .word_size = 1, |
| 55 | .reg_read = qfprom_reg_read, |
| 56 | .reg_write = qfprom_reg_write, |
| 57 | }; |
| 58 | |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 59 | static int qfprom_probe(struct platform_device *pdev) |
| 60 | { |
| 61 | struct device *dev = &pdev->dev; |
| 62 | struct resource *res; |
| 63 | struct nvmem_device *nvmem; |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 64 | struct qfprom_priv *priv; |
| 65 | |
| 66 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 67 | if (!priv) |
| 68 | return -ENOMEM; |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 69 | |
| 70 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 71 | priv->base = devm_ioremap_resource(dev, res); |
| 72 | if (IS_ERR(priv->base)) |
| 73 | return PTR_ERR(priv->base); |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 74 | |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 75 | econfig.size = resource_size(res); |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 76 | econfig.dev = dev; |
Masahiro Yamada | ec3672b | 2017-10-21 01:57:41 +0900 | [diff] [blame] | 77 | econfig.priv = priv; |
Srinivas Kandagatla | 382c62f | 2016-04-24 20:28:08 +0100 | [diff] [blame] | 78 | |
Andrey Smirnov | e5692ef | 2018-03-09 14:47:09 +0000 | [diff] [blame] | 79 | nvmem = devm_nvmem_register(dev, &econfig); |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 80 | |
Andrey Smirnov | e5692ef | 2018-03-09 14:47:09 +0000 | [diff] [blame] | 81 | return PTR_ERR_OR_ZERO(nvmem); |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static const struct of_device_id qfprom_of_match[] = { |
| 85 | { .compatible = "qcom,qfprom",}, |
| 86 | {/* sentinel */}, |
| 87 | }; |
| 88 | MODULE_DEVICE_TABLE(of, qfprom_of_match); |
| 89 | |
| 90 | static struct platform_driver qfprom_driver = { |
| 91 | .probe = qfprom_probe, |
Srinivas Kandagatla | 4ab1199 | 2015-07-27 12:15:00 +0100 | [diff] [blame] | 92 | .driver = { |
| 93 | .name = "qcom,qfprom", |
| 94 | .of_match_table = qfprom_of_match, |
| 95 | }, |
| 96 | }; |
| 97 | module_platform_driver(qfprom_driver); |
| 98 | MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>"); |
| 99 | MODULE_DESCRIPTION("Qualcomm QFPROM driver"); |
| 100 | MODULE_LICENSE("GPL v2"); |