Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
c1ae18d
Documentation
LICENSES
arch
block
certs
crypto
drivers
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cdrom
char
clk
clocksource
connector
counter
cpufreq
cpuidle
crypto
dax
dca
devfreq
dio
dma-buf
dma
edac
eisa
extcon
firewire
firmware
fpga
fsi
gnss
gpio
gpu
greybus
hid
hsi
hv
hwmon
occ
pmbus
Kconfig
Makefile
ab8500.c
abituguru.c
abituguru3.c
abx500.c
abx500.h
acpi_power_meter.c
ad7314.c
ad7414.c
ad7418.c
adc128d818.c
adcxx.c
adm1021.c
adm1025.c
adm1026.c
adm1029.c
adm1031.c
adm1177.c
adm9240.c
ads7828.c
ads7871.c
adt7310.c
adt7410.c
adt7411.c
adt7462.c
adt7470.c
adt7475.c
adt7x10.c
adt7x10.h
amc6821.c
amd_energy.c
applesmc.c
as370-hwmon.c
asb100.c
asc7621.c
aspeed-pwm-tacho.c
asus_atk0110.c
atxp1.c
axi-fan-control.c
bt1-pvt.c
bt1-pvt.h
coretemp.c
corsair-cpro.c
da9052-hwmon.c
da9055-hwmon.c
dell-smm-hwmon.c
dme1737.c
drivetemp.c
ds1621.c
ds620.c
emc1403.c
emc2103.c
emc6w201.c
f71805f.c
f71882fg.c
f75375s.c
fam15h_power.c
fschmd.c
ftsteutates.c
g760a.c
g762.c
gl518sm.c
gl520sm.c
gpio-fan.c
gsc-hwmon.c
hih6130.c
hwmon-vid.c
hwmon.c
i5500_temp.c
i5k_amb.c
ibmaem.c
ibmpex.c
ibmpowernv.c
iio_hwmon.c
ina209.c
ina2xx.c
ina3221.c
it87.c
jc42.c
k10temp.c
k8temp.c
lineage-pem.c
lm63.c
lm70.c
lm73.c
lm75.c
lm75.h
lm77.c
lm78.c
lm80.c
lm83.c
lm85.c
lm87.c
lm90.c
lm92.c
lm93.c
lm95234.c
lm95241.c
lm95245.c
lochnagar-hwmon.c
ltc2945.c
ltc2947-core.c
ltc2947-i2c.c
ltc2947-spi.c
ltc2947.h
ltc2990.c
ltc4151.c
ltc4215.c
ltc4222.c
ltc4245.c
ltc4260.c
ltc4261.c
ltq-cputemp.c
max1111.c
max16065.c
max1619.c
max1668.c
max197.c
max31722.c
max31730.c
max31790.c
max6621.c
max6639.c
max6642.c
max6650.c
max6697.c
mc13783-adc.c
mcp3021.c
menf21bmc_hwmon.c
mlxreg-fan.c
nct6683.c
nct6775.c
nct7802.c
nct7904.c
npcm750-pwm-fan.c
nsa320-hwmon.c
ntc_thermistor.c
pc87360.c
pc87427.c
pcf8591.c
powr1220.c
pwm-fan.c
raspberrypi-hwmon.c
s3c-hwmon.c
sch5627.c
sch5636.c
sch56xx-common.c
sch56xx-common.h
scmi-hwmon.c
scpi-hwmon.c
sht15.c
sht21.c
sht3x.c
shtc1.c
sis5595.c
smm665.c
smsc47b397.c
smsc47m1.c
smsc47m192.c
sparx5-temp.c
stts751.c
tc654.c
tc74.c
thmc50.c
tmp102.c
tmp103.c
tmp108.c
tmp401.c
tmp421.c
tmp513.c
ultra45_env.c
vexpress-hwmon.c
via-cputemp.c
via686a.c
vt1211.c
vt8231.c
w83627ehf.c
w83627hf.c
w83773g.c
w83781d.c
w83791d.c
w83792d.c
w83793.c
w83795.c
w83l785ts.c
w83l786ng.c
wm831x-hwmon.c
wm8350-hwmon.c
xgene-hwmon.c
hwspinlock
hwtracing
i2c
i3c
ide
idle
iio
infiniband
input
interconnect
iommu
ipack
irqchip
isdn
leds
lightnvm
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
mmc
most
mtd
mux
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
opp
oprofile
parisc
parport
pci
pcmcia
perf
phy
pinctrl
platform
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
s390
sbus
scsi
sfi
sh
siox
slimbus
soc
soundwire
spi
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty
uio
usb
vdpa
vfio
vhost
video
virt
virtio
visorbus
vlynq
vme
w1
watchdog
xen
zorro
Kconfig
Makefile
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
drivers
/
hwmon
/
gsc-hwmon.c
Copy path
Blame
Blame
Latest commit
History
History
393 lines (338 loc) · 10.5 KB
Breadcrumbs
linux
/
drivers
/
hwmon
/
gsc-hwmon.c
Top
File metadata and controls
Code
Blame
393 lines (338 loc) · 10.5 KB
Raw
// SPDX-License-Identifier: GPL-2.0 /* * Driver for Gateworks System Controller Hardware Monitor module * * Copyright (C) 2020 Gateworks Corporation */ #include <linux/hwmon.h> #include <linux/hwmon-sysfs.h> #include <linux/mfd/gsc.h> #include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <linux/slab.h> #include <linux/platform_data/gsc_hwmon.h> #define GSC_HWMON_MAX_TEMP_CH 16 #define GSC_HWMON_MAX_IN_CH 16 #define GSC_HWMON_RESOLUTION 12 #define GSC_HWMON_VREF 2500 struct gsc_hwmon_data { struct gsc_dev *gsc; struct gsc_hwmon_platform_data *pdata; struct regmap *regmap; const struct gsc_hwmon_channel *temp_ch[GSC_HWMON_MAX_TEMP_CH]; const struct gsc_hwmon_channel *in_ch[GSC_HWMON_MAX_IN_CH]; u32 temp_config[GSC_HWMON_MAX_TEMP_CH + 1]; u32 in_config[GSC_HWMON_MAX_IN_CH + 1]; struct hwmon_channel_info temp_info; struct hwmon_channel_info in_info; const struct hwmon_channel_info *info[3]; struct hwmon_chip_info chip; }; static struct regmap_bus gsc_hwmon_regmap_bus = { .reg_read = gsc_read, .reg_write = gsc_write, }; static const struct regmap_config gsc_hwmon_regmap_config = { .reg_bits = 8, .val_bits = 8, .cache_type = REGCACHE_NONE, }; static ssize_t pwm_auto_point_temp_show(struct device *dev, struct device_attribute *devattr, char *buf) { struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); u8 reg = hwmon->pdata->fan_base + (2 * attr->index); u8 regs[2]; int ret; ret = regmap_bulk_read(hwmon->regmap, reg, regs, 2); if (ret) return ret; ret = regs[0] | regs[1] << 8; return sprintf(buf, "%d\n", ret * 10); } static ssize_t pwm_auto_point_temp_store(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); u8 reg = hwmon->pdata->fan_base + (2 * attr->index); u8 regs[2]; long temp; int err; if (kstrtol(buf, 10, &temp)) return -EINVAL; temp = clamp_val(temp, 0, 10000); temp = DIV_ROUND_CLOSEST(temp, 10); regs[0] = temp & 0xff; regs[1] = (temp >> 8) & 0xff; err = regmap_bulk_write(hwmon->regmap, reg, regs, 2); if (err) return err; return count; } static ssize_t pwm_auto_point_pwm_show(struct device *dev, struct device_attribute *devattr, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); return sprintf(buf, "%d\n", 255 * (50 + (attr->index * 10)) / 100); } static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0); static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0); static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1); static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1); static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2); static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2); static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3); static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3); static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4); static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4); static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5); static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5); static struct attribute *gsc_hwmon_attributes[] = { &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr, &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr, NULL }; static const struct attribute_group gsc_hwmon_group = { .attrs = gsc_hwmon_attributes, }; __ATTRIBUTE_GROUPS(gsc_hwmon); static int gsc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev); const struct gsc_hwmon_channel *ch; int sz, ret; long tmp; u8 buf[3]; switch (type) { case hwmon_in: ch = hwmon->in_ch[channel]; break; case hwmon_temp: ch = hwmon->temp_ch[channel]; break; default: return -EOPNOTSUPP; } sz = (ch->mode == mode_voltage_24bit) ? 3 : 2; ret = regmap_bulk_read(hwmon->regmap, ch->reg, buf, sz); if (ret) return ret; tmp = 0; while (sz-- > 0) tmp |= (buf[sz] << (8 * sz)); switch (ch->mode) { case mode_temperature: if (tmp > 0x8000) tmp -= 0xffff; tmp *= 100; /* convert to millidegrees celsius */ break; case mode_voltage_raw: tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION)); /* scale based on ref voltage and ADC resolution */ tmp *= GSC_HWMON_VREF; tmp >>= GSC_HWMON_RESOLUTION; /* scale based on optional voltage divider */ if (ch->vdiv[0] && ch->vdiv[1]) { tmp *= (ch->vdiv[0] + ch->vdiv[1]); tmp /= ch->vdiv[1]; } /* adjust by uV offset */ tmp += ch->mvoffset; break; case mode_voltage_24bit: case mode_voltage_16bit: /* no adjustment needed */ break; } *val = tmp; return 0; } static int gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, const char **buf) { struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev); switch (type) { case hwmon_in: *buf = hwmon->in_ch[channel]->name; break; case hwmon_temp: *buf = hwmon->temp_ch[channel]->name; break; default: return -ENOTSUPP; } return 0; } static umode_t gsc_hwmon_is_visible(const void *_data, enum hwmon_sensor_types type, u32 attr, int ch) { return 0444; } static const struct hwmon_ops gsc_hwmon_ops = { .is_visible = gsc_hwmon_is_visible, .read = gsc_hwmon_read, .read_string = gsc_hwmon_read_string, }; static struct gsc_hwmon_platform_data * gsc_hwmon_get_devtree_pdata(struct device *dev) { struct gsc_hwmon_platform_data *pdata; struct gsc_hwmon_channel *ch; struct fwnode_handle *child; struct device_node *fan; int nchannels; nchannels = device_get_child_node_count(dev); if (nchannels == 0) return ERR_PTR(-ENODEV); pdata = devm_kzalloc(dev, sizeof(*pdata) + nchannels * sizeof(*ch), GFP_KERNEL); if (!pdata) return ERR_PTR(-ENOMEM); ch = (struct gsc_hwmon_channel *)(pdata + 1); pdata->channels = ch; pdata->nchannels = nchannels; /* fan controller base address */ fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan"); if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) { dev_err(dev, "fan node without base\n"); return ERR_PTR(-EINVAL); } /* allocate structures for channels and count instances of each type */ device_for_each_child_node(dev, child) { if (fwnode_property_read_string(child, "label", &ch->name)) { dev_err(dev, "channel without label\n"); fwnode_handle_put(child); return ERR_PTR(-EINVAL); } if (fwnode_property_read_u32(child, "reg", &ch->reg)) { dev_err(dev, "channel without reg\n"); fwnode_handle_put(child); return ERR_PTR(-EINVAL); } if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) { dev_err(dev, "channel without mode\n"); fwnode_handle_put(child); return ERR_PTR(-EINVAL); } if (ch->mode > mode_max) { dev_err(dev, "invalid channel mode\n"); fwnode_handle_put(child); return ERR_PTR(-EINVAL); } if (!fwnode_property_read_u32(child, "gw,voltage-offset-microvolt", &ch->mvoffset)) ch->mvoffset /= 1000; fwnode_property_read_u32_array(child, "gw,voltage-divider-ohms", ch->vdiv, ARRAY_SIZE(ch->vdiv)); ch++; } return pdata; } static int gsc_hwmon_probe(struct platform_device *pdev) { struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent); struct device *dev = &pdev->dev; struct device *hwmon_dev; struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev); struct gsc_hwmon_data *hwmon; const struct attribute_group **groups; int i, i_in, i_temp; if (!pdata) { pdata = gsc_hwmon_get_devtree_pdata(dev); if (IS_ERR(pdata)) return PTR_ERR(pdata); } hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL); if (!hwmon) return -ENOMEM; hwmon->gsc = gsc; hwmon->pdata = pdata; hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus, gsc->i2c_hwmon, &gsc_hwmon_regmap_config); if (IS_ERR(hwmon->regmap)) return PTR_ERR(hwmon->regmap); for (i = 0, i_in = 0, i_temp = 0; i < hwmon->pdata->nchannels; i++) { const struct gsc_hwmon_channel *ch = &pdata->channels[i]; switch (ch->mode) { case mode_temperature: if (i_temp == GSC_HWMON_MAX_TEMP_CH) { dev_err(gsc->dev, "too many temp channels\n"); return -EINVAL; } hwmon->temp_ch[i_temp] = ch; hwmon->temp_config[i_temp] = HWMON_T_INPUT | HWMON_T_LABEL; i_temp++; break; case mode_voltage_24bit: case mode_voltage_16bit: case mode_voltage_raw: if (i_in == GSC_HWMON_MAX_IN_CH) { dev_err(gsc->dev, "too many input channels\n"); return -EINVAL; } hwmon->in_ch[i_in] = ch; hwmon->in_config[i_in] = HWMON_I_INPUT | HWMON_I_LABEL; i_in++; break; default: dev_err(gsc->dev, "invalid mode: %d\n", ch->mode); return -EINVAL; } } /* setup config structures */ hwmon->chip.ops = &gsc_hwmon_ops; hwmon->chip.info = hwmon->info; hwmon->info[0] = &hwmon->temp_info; hwmon->info[1] = &hwmon->in_info; hwmon->temp_info.type = hwmon_temp; hwmon->temp_info.config = hwmon->temp_config; hwmon->in_info.type = hwmon_in; hwmon->in_info.config = hwmon->in_config; groups = pdata->fan_base ? gsc_hwmon_groups : NULL; hwmon_dev = devm_hwmon_device_register_with_info(dev, KBUILD_MODNAME, hwmon, &hwmon->chip, groups); return PTR_ERR_OR_ZERO(hwmon_dev); } static const struct of_device_id gsc_hwmon_of_match[] = { { .compatible = "gw,gsc-adc", }, {} }; static struct platform_driver gsc_hwmon_driver = { .driver = { .name = "gsc-hwmon", .of_match_table = gsc_hwmon_of_match, }, .probe = gsc_hwmon_probe, }; module_platform_driver(gsc_hwmon_driver); MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>"); MODULE_DESCRIPTION("GSC hardware monitor driver"); MODULE_LICENSE("GPL v2");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
You can’t perform that action at this time.