1 /* Hwmon client for industrial I/O devices
3 * Copyright (c) 2011 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/iio/consumer.h>
18 #include <linux/iio/types.h>
21 * struct iio_hwmon_state - device instance state
22 * @channels: filled with array of channels from iio
23 * @num_channels: number of channels in channels (saves counting twice)
24 * @hwmon_dev: associated hwmon device
25 * @attr_group: the group of attributes
26 * @attrs: null terminated array of attribute pointers.
28 struct iio_hwmon_state
{
29 struct iio_channel
*channels
;
31 struct device
*hwmon_dev
;
32 struct attribute_group attr_group
;
33 struct attribute
**attrs
;
37 * Assumes that IIO and hwmon operate in the same base units.
38 * This is supposed to be true, but needs verification for
41 static ssize_t
iio_hwmon_read_val(struct device
*dev
,
42 struct device_attribute
*attr
,
46 int val
, ret
, scaleint
, scalepart
;
47 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
48 struct iio_hwmon_state
*state
= dev_get_drvdata(dev
);
51 * No locking between this pair, so theoretically possible
52 * the scale has changed.
54 ret
= iio_read_channel_raw(&state
->channels
[sattr
->index
],
59 ret
= iio_read_channel_scale(&state
->channels
[sattr
->index
],
60 &scaleint
, &scalepart
);
65 result
= val
* scaleint
;
67 case IIO_VAL_INT_PLUS_MICRO
:
68 result
= (s64
)val
* (s64
)scaleint
+
69 div_s64((s64
)val
* (s64
)scalepart
, 1000000LL);
71 case IIO_VAL_INT_PLUS_NANO
:
72 result
= (s64
)val
* (s64
)scaleint
+
73 div_s64((s64
)val
* (s64
)scalepart
, 1000000000LL);
78 return sprintf(buf
, "%ld\n", result
);
81 static void iio_hwmon_free_attrs(struct iio_hwmon_state
*st
)
84 struct sensor_device_attribute
*a
;
85 for (i
= 0; i
< st
->num_channels
; i
++)
87 a
= to_sensor_dev_attr(
88 container_of(st
->attrs
[i
],
89 struct device_attribute
,
95 static int __devinit
iio_hwmon_probe(struct platform_device
*pdev
)
97 struct iio_hwmon_state
*st
;
98 struct sensor_device_attribute
*a
;
100 int in_i
= 1, temp_i
= 1, curr_i
= 1;
101 enum iio_chan_type type
;
103 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
109 st
->channels
= iio_channel_get_all(dev_name(&pdev
->dev
));
110 if (IS_ERR(st
->channels
)) {
111 ret
= PTR_ERR(st
->channels
);
112 goto error_free_state
;
115 /* count how many attributes we have */
116 while (st
->channels
[st
->num_channels
].indio_dev
)
119 st
->attrs
= kzalloc(sizeof(st
->attrs
) * (st
->num_channels
+ 1),
121 if (st
->attrs
== NULL
) {
123 goto error_release_channels
;
125 for (i
= 0; i
< st
->num_channels
; i
++) {
126 a
= kzalloc(sizeof(*a
), GFP_KERNEL
);
129 goto error_free_attrs
;
132 sysfs_attr_init(&a
->dev_attr
.attr
);
133 ret
= iio_get_channel_type(&st
->channels
[i
], &type
);
136 goto error_free_attrs
;
140 a
->dev_attr
.attr
.name
= kasprintf(GFP_KERNEL
,
145 a
->dev_attr
.attr
.name
= kasprintf(GFP_KERNEL
,
150 a
->dev_attr
.attr
.name
= kasprintf(GFP_KERNEL
,
157 goto error_free_attrs
;
159 if (a
->dev_attr
.attr
.name
== NULL
) {
162 goto error_free_attrs
;
164 a
->dev_attr
.show
= iio_hwmon_read_val
;
165 a
->dev_attr
.attr
.mode
= S_IRUGO
;
167 st
->attrs
[i
] = &a
->dev_attr
.attr
;
170 st
->attr_group
.attrs
= st
->attrs
;
171 platform_set_drvdata(pdev
, st
);
172 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &st
->attr_group
);
174 goto error_free_attrs
;
176 st
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
177 if (IS_ERR(st
->hwmon_dev
)) {
178 ret
= PTR_ERR(st
->hwmon_dev
);
179 goto error_remove_group
;
184 sysfs_remove_group(&pdev
->dev
.kobj
, &st
->attr_group
);
186 iio_hwmon_free_attrs(st
);
188 error_release_channels
:
189 iio_channel_release_all(st
->channels
);
196 static int __devexit
iio_hwmon_remove(struct platform_device
*pdev
)
198 struct iio_hwmon_state
*st
= platform_get_drvdata(pdev
);
200 hwmon_device_unregister(st
->hwmon_dev
);
201 sysfs_remove_group(&pdev
->dev
.kobj
, &st
->attr_group
);
202 iio_hwmon_free_attrs(st
);
204 iio_channel_release_all(st
->channels
);
209 static struct platform_driver __refdata iio_hwmon_driver
= {
212 .owner
= THIS_MODULE
,
214 .probe
= iio_hwmon_probe
,
215 .remove
= __devexit_p(iio_hwmon_remove
),
218 module_platform_driver(iio_hwmon_driver
);
220 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
221 MODULE_DESCRIPTION("IIO to hwmon driver");
222 MODULE_LICENSE("GPL v2");