|
| 1 | +/* |
| 2 | + * Copyright Altera Corporation (C) 2013. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms and conditions of the GNU General Public License, |
| 6 | + * version 2, as published by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | + * more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + * |
| 16 | + * Credit: |
| 17 | + * Walter Goossens |
| 18 | + */ |
| 19 | + |
| 20 | +#include <linux/kernel.h> |
| 21 | +#include <linux/module.h> |
| 22 | +#include <linux/device.h> |
| 23 | +#include <linux/platform_device.h> |
| 24 | +#include <linux/slab.h> |
| 25 | +#include <linux/io.h> |
| 26 | +#include <linux/of.h> |
| 27 | + |
| 28 | +#define DRV_NAME "altera_sysid" |
| 29 | + |
| 30 | +struct altera_sysid { |
| 31 | + void __iomem *regs; |
| 32 | +}; |
| 33 | + |
| 34 | +/* System ID Registers*/ |
| 35 | +#define SYSID_REG_ID (0x0) |
| 36 | +#define SYSID_REG_TIMESTAMP (0x4) |
| 37 | + |
| 38 | +static ssize_t altera_sysid_show_id(struct device *dev, |
| 39 | + struct device_attribute *attr, char *buf) |
| 40 | +{ |
| 41 | + struct altera_sysid *sysid = dev_get_drvdata(dev); |
| 42 | + |
| 43 | + return sprintf(buf, "%u\n", readl(sysid->regs + SYSID_REG_ID)); |
| 44 | +} |
| 45 | + |
| 46 | +static ssize_t altera_sysid_show_timestamp(struct device *dev, |
| 47 | + struct device_attribute *attr, char *buf) |
| 48 | +{ |
| 49 | + unsigned int reg; |
| 50 | + struct tm timestamp; |
| 51 | + struct altera_sysid *sysid = dev_get_drvdata(dev); |
| 52 | + |
| 53 | + reg = readl(sysid->regs + SYSID_REG_TIMESTAMP); |
| 54 | + |
| 55 | + time64_to_tm(reg, 0, ×tamp); |
| 56 | + |
| 57 | + return sprintf(buf, "%u (%u-%u-%u %u:%u:%u UTC)\n", reg, |
| 58 | + (unsigned int)(timestamp.tm_year + 1900), |
| 59 | + timestamp.tm_mon + 1, timestamp.tm_mday, timestamp.tm_hour, |
| 60 | + timestamp.tm_min, timestamp.tm_sec); |
| 61 | +} |
| 62 | + |
| 63 | +static DEVICE_ATTR(id, S_IRUGO, altera_sysid_show_id, NULL); |
| 64 | +static DEVICE_ATTR(timestamp, S_IRUGO, altera_sysid_show_timestamp, NULL); |
| 65 | + |
| 66 | +static struct attribute *altera_sysid_attrs[] = { |
| 67 | + &dev_attr_id.attr, |
| 68 | + &dev_attr_timestamp.attr, |
| 69 | + NULL, |
| 70 | +}; |
| 71 | + |
| 72 | +struct attribute_group altera_sysid_attr_group = { |
| 73 | + .name = "sysid", |
| 74 | + .attrs = altera_sysid_attrs, |
| 75 | +}; |
| 76 | + |
| 77 | +static int altera_sysid_probe(struct platform_device *pdev) |
| 78 | +{ |
| 79 | + struct altera_sysid *sysid; |
| 80 | + struct resource *regs; |
| 81 | + |
| 82 | + sysid = devm_kzalloc(&pdev->dev, sizeof(struct altera_sysid), |
| 83 | + GFP_KERNEL); |
| 84 | + if (!sysid) |
| 85 | + return -ENOMEM; |
| 86 | + |
| 87 | + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 88 | + if (!regs) |
| 89 | + return -ENXIO; |
| 90 | + |
| 91 | + sysid->regs = devm_ioremap_resource(&pdev->dev, regs); |
| 92 | + if (IS_ERR(sysid->regs)) |
| 93 | + return PTR_ERR(sysid->regs); |
| 94 | + |
| 95 | + platform_set_drvdata(pdev, sysid); |
| 96 | + |
| 97 | + return sysfs_create_group(&pdev->dev.kobj, &altera_sysid_attr_group); |
| 98 | +} |
| 99 | + |
| 100 | +static int altera_sysid_remove(struct platform_device *pdev) |
| 101 | +{ |
| 102 | + sysfs_remove_group(&pdev->dev.kobj, &altera_sysid_attr_group); |
| 103 | + |
| 104 | + platform_set_drvdata(pdev, NULL); |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +static const struct of_device_id altera_sysid_match[] = { |
| 109 | + { .compatible = "altr,sysid-1.0" }, |
| 110 | + { /* Sentinel */ } |
| 111 | +}; |
| 112 | + |
| 113 | +MODULE_DEVICE_TABLE(of, altera_sysid_match); |
| 114 | + |
| 115 | +static struct platform_driver altera_sysid_platform_driver = { |
| 116 | + .driver = { |
| 117 | + .name = DRV_NAME, |
| 118 | + .owner = THIS_MODULE, |
| 119 | + .of_match_table = of_match_ptr(altera_sysid_match), |
| 120 | + }, |
| 121 | + .probe = altera_sysid_probe, |
| 122 | + .remove = altera_sysid_remove, |
| 123 | +}; |
| 124 | + |
| 125 | +static int __init altera_sysid_init(void) |
| 126 | +{ |
| 127 | + return platform_driver_register(&altera_sysid_platform_driver); |
| 128 | +} |
| 129 | + |
| 130 | +static void __exit altera_sysid_exit(void) |
| 131 | +{ |
| 132 | + platform_driver_unregister(&altera_sysid_platform_driver); |
| 133 | +} |
| 134 | + |
| 135 | +module_init(altera_sysid_init); |
| 136 | +module_exit(altera_sysid_exit); |
| 137 | + |
| 138 | +MODULE_AUTHOR( "Ley Foon Tan <[email protected]>"); |
| 139 | +MODULE_LICENSE("GPL v2"); |
| 140 | +MODULE_DESCRIPTION("Altera System ID driver"); |
| 141 | +MODULE_ALIAS("platform:" DRV_NAME); |
0 commit comments