^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* linux/drivers/hwmon/s3c-hwmon.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2005, 2008, 2009 Simtec Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * http://armlinux.simtec.co.uk/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Ben Dooks <ben@simtec.co.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * S3C24XX/S3C64XX ADC hwmon support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/soc/samsung/s3c-adc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/platform_data/hwmon-s3c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct s3c_hwmon_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct sensor_device_attribute in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct sensor_device_attribute label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char in_name[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) char label_name[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * struct s3c_hwmon - ADC hwmon client information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @lock: Access lock to serialise the conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @client: The client we registered with the S3C ADC core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @hwmon_dev: The hwmon device we created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @attr: The holders for the channel attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct s3c_hwmon {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct s3c_adc_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct s3c_hwmon_attr attrs[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * s3c_hwmon_read_ch - read a value from a given adc channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @dev: The device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @hwmon: Our state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @channel: The channel we're reading from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Read a value from the @channel with the proper locking and sleep until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * either the read completes or we timeout awaiting the ADC core to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * back to us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int s3c_hwmon_read_ch(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct s3c_hwmon *hwmon, int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = mutex_lock_interruptible(&hwmon->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) dev_dbg(dev, "reading channel %d\n", channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ret = s3c_adc_read(hwmon->client, channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) mutex_unlock(&hwmon->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #ifdef CONFIG_SENSORS_S3C_RAW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * s3c_hwmon_show_raw - show a conversion from the raw channel number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @dev: The device that the attribute belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @attr: The attribute being read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @buf: The result buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * This show deals with the raw attribute, registered for each possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * ADC channel. This does a conversion and returns the raw (un-scaled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * value returned from the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static ssize_t s3c_hwmon_show_raw(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct s3c_hwmon *adc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct sensor_device_attribute *sa = to_sensor_dev_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = s3c_hwmon_read_ch(dev, adc, sa->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return (ret < 0) ? ret : snprintf(buf, PAGE_SIZE, "%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static SENSOR_DEVICE_ATTR(adc0_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static SENSOR_DEVICE_ATTR(adc1_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static SENSOR_DEVICE_ATTR(adc2_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static SENSOR_DEVICE_ATTR(adc3_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static SENSOR_DEVICE_ATTR(adc4_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static SENSOR_DEVICE_ATTR(adc5_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static SENSOR_DEVICE_ATTR(adc6_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static SENSOR_DEVICE_ATTR(adc7_raw, S_IRUGO, s3c_hwmon_show_raw, NULL, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static struct attribute *s3c_hwmon_attrs[9] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) &sensor_dev_attr_adc0_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) &sensor_dev_attr_adc1_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) &sensor_dev_attr_adc2_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) &sensor_dev_attr_adc3_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) &sensor_dev_attr_adc4_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) &sensor_dev_attr_adc5_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) &sensor_dev_attr_adc6_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) &sensor_dev_attr_adc7_raw.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static struct attribute_group s3c_hwmon_attrgroup = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .attrs = s3c_hwmon_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static inline int s3c_hwmon_add_raw(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return sysfs_create_group(&dev->kobj, &s3c_hwmon_attrgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static inline void s3c_hwmon_remove_raw(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sysfs_remove_group(&dev->kobj, &s3c_hwmon_attrgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static inline int s3c_hwmon_add_raw(struct device *dev) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline void s3c_hwmon_remove_raw(struct device *dev) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #endif /* CONFIG_SENSORS_S3C_RAW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * s3c_hwmon_ch_show - show value of a given channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @dev: The device that the attribute belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @attr: The attribute being read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @buf: The result buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Read a value from the ADC and scale it before returning it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * caller. The scale factor is gained from the channel configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * passed via the platform data when the device was registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static ssize_t s3c_hwmon_ch_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct s3c_hwmon *hwmon = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct s3c_hwmon_chcfg *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) cfg = pdata->in[sen_attr->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = s3c_hwmon_read_ch(dev, hwmon, sen_attr->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret *= cfg->mult;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = DIV_ROUND_CLOSEST(ret, cfg->div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return snprintf(buf, PAGE_SIZE, "%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * s3c_hwmon_label_show - show label name of the given channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * @dev: The device that the attribute belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * @attr: The attribute being read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @buf: The result buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * Return the label name of a given channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static ssize_t s3c_hwmon_label_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct sensor_device_attribute *sen_attr = to_sensor_dev_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct s3c_hwmon_pdata *pdata = dev_get_platdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct s3c_hwmon_chcfg *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) cfg = pdata->in[sen_attr->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return snprintf(buf, PAGE_SIZE, "%s\n", cfg->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * s3c_hwmon_create_attr - create hwmon attribute for given channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * @dev: The device to create the attribute on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * @cfg: The channel configuration passed from the platform data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @channel: The ADC channel number to process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Create the scaled attribute for use with hwmon from the specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * platform data in @pdata. The sysfs entry is handled by the routine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * s3c_hwmon_ch_show().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * The attribute name is taken from the configuration data if present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * otherwise the name is taken by concatenating in_ with the channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int s3c_hwmon_create_attr(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct s3c_hwmon_chcfg *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct s3c_hwmon_attr *attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct sensor_device_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) snprintf(attrs->in_name, sizeof(attrs->in_name), "in%d_input", channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) attr = &attrs->in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) attr->index = channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) sysfs_attr_init(&attr->dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) attr->dev_attr.attr.name = attrs->in_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) attr->dev_attr.attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) attr->dev_attr.show = s3c_hwmon_ch_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ret = device_create_file(dev, &attr->dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_err(dev, "failed to create input attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* if this has a name, add a label */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (cfg->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) snprintf(attrs->label_name, sizeof(attrs->label_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) "in%d_label", channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) attr = &attrs->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) attr->index = channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) sysfs_attr_init(&attr->dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) attr->dev_attr.attr.name = attrs->label_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) attr->dev_attr.attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) attr->dev_attr.show = s3c_hwmon_label_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = device_create_file(dev, &attr->dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) device_remove_file(dev, &attrs->in.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) dev_err(dev, "failed to create label attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void s3c_hwmon_remove_attr(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct s3c_hwmon_attr *attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) device_remove_file(dev, &attrs->in.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) device_remove_file(dev, &attrs->label.dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * s3c_hwmon_probe - device probe entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @dev: The device being probed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static int s3c_hwmon_probe(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct s3c_hwmon_pdata *pdata = dev_get_platdata(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct s3c_hwmon *hwmon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_err(&dev->dev, "no platform data supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) hwmon = devm_kzalloc(&dev->dev, sizeof(struct s3c_hwmon), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (hwmon == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) platform_set_drvdata(dev, hwmon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) mutex_init(&hwmon->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* Register with the core ADC driver. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) hwmon->client = s3c_adc_register(dev, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (IS_ERR(hwmon->client)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev_err(&dev->dev, "cannot register adc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return PTR_ERR(hwmon->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* add attributes for our adc devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ret = s3c_hwmon_add_raw(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto err_registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* register with the hwmon core */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) hwmon->hwmon_dev = hwmon_device_register(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (IS_ERR(hwmon->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) dev_err(&dev->dev, "error registering with hwmon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) ret = PTR_ERR(hwmon->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) goto err_raw_attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) for (i = 0; i < ARRAY_SIZE(pdata->in); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct s3c_hwmon_chcfg *cfg = pdata->in[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (!cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (cfg->mult >= 0x10000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) dev_warn(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) "channel %d multiplier too large\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (cfg->div == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) dev_err(&dev->dev, "channel %d divider zero\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ret = s3c_hwmon_create_attr(&dev->dev, pdata->in[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) &hwmon->attrs[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) "error creating channel %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) for (i--; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) s3c_hwmon_remove_attr(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) &hwmon->attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto err_hwmon_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) err_hwmon_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) hwmon_device_unregister(hwmon->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err_raw_attribute:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) s3c_hwmon_remove_raw(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err_registered:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) s3c_adc_release(hwmon->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int s3c_hwmon_remove(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct s3c_hwmon *hwmon = platform_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) s3c_hwmon_remove_raw(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) for (i = 0; i < ARRAY_SIZE(hwmon->attrs); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) s3c_hwmon_remove_attr(&dev->dev, &hwmon->attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) hwmon_device_unregister(hwmon->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) s3c_adc_release(hwmon->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static struct platform_driver s3c_hwmon_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) .name = "s3c-hwmon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .probe = s3c_hwmon_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .remove = s3c_hwmon_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) module_platform_driver(s3c_hwmon_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_DESCRIPTION("S3C ADC HWMon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) MODULE_ALIAS("platform:s3c-hwmon");