|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* Copyright (C) 2014-2019 aQuantia Corporation. */ |
| 3 | + |
| 4 | +/* File aq_drvinfo.c: Definition of common code for firmware info in sys.*/ |
| 5 | + |
| 6 | +#include <linux/init.h> |
| 7 | +#include <linux/kobject.h> |
| 8 | +#include <linux/module.h> |
| 9 | +#include <linux/stat.h> |
| 10 | +#include <linux/string.h> |
| 11 | +#include <linux/hwmon.h> |
| 12 | +#include <linux/uaccess.h> |
| 13 | + |
| 14 | +#include "aq_drvinfo.h" |
| 15 | + |
| 16 | +static int aq_hwmon_read(struct device *dev, enum hwmon_sensor_types type, |
| 17 | + u32 attr, int channel, long *value) |
| 18 | +{ |
| 19 | + struct aq_nic_s *aq_nic = dev_get_drvdata(dev); |
| 20 | + int temp; |
| 21 | + int err; |
| 22 | + |
| 23 | + if (!aq_nic) |
| 24 | + return -EIO; |
| 25 | + |
| 26 | + if (type != hwmon_temp) |
| 27 | + return -EOPNOTSUPP; |
| 28 | + |
| 29 | + if (!aq_nic->aq_fw_ops->get_phy_temp) |
| 30 | + return -EOPNOTSUPP; |
| 31 | + |
| 32 | + switch (attr) { |
| 33 | + case hwmon_temp_input: |
| 34 | + err = aq_nic->aq_fw_ops->get_phy_temp(aq_nic->aq_hw, &temp); |
| 35 | + *value = temp; |
| 36 | + return err; |
| 37 | + default: |
| 38 | + return -EOPNOTSUPP; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +static int aq_hwmon_read_string(struct device *dev, |
| 43 | + enum hwmon_sensor_types type, |
| 44 | + u32 attr, int channel, const char **str) |
| 45 | +{ |
| 46 | + struct aq_nic_s *aq_nic = dev_get_drvdata(dev); |
| 47 | + |
| 48 | + if (!aq_nic) |
| 49 | + return -EIO; |
| 50 | + |
| 51 | + if (type != hwmon_temp) |
| 52 | + return -EOPNOTSUPP; |
| 53 | + |
| 54 | + if (!aq_nic->aq_fw_ops->get_phy_temp) |
| 55 | + return -EOPNOTSUPP; |
| 56 | + |
| 57 | + switch (attr) { |
| 58 | + case hwmon_temp_label: |
| 59 | + *str = "PHY Temperature"; |
| 60 | + return 0; |
| 61 | + default: |
| 62 | + return -EOPNOTSUPP; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +static umode_t aq_hwmon_is_visible(const void *data, |
| 67 | + enum hwmon_sensor_types type, |
| 68 | + u32 attr, int channel) |
| 69 | +{ |
| 70 | + if (type != hwmon_temp) |
| 71 | + return 0; |
| 72 | + |
| 73 | + switch (attr) { |
| 74 | + case hwmon_temp_input: |
| 75 | + case hwmon_temp_label: |
| 76 | + return 0444; |
| 77 | + default: |
| 78 | + return 0; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +static const struct hwmon_ops aq_hwmon_ops = { |
| 83 | + .is_visible = aq_hwmon_is_visible, |
| 84 | + .read = aq_hwmon_read, |
| 85 | + .read_string = aq_hwmon_read_string, |
| 86 | +}; |
| 87 | + |
| 88 | +static u32 aq_hwmon_temp_config[] = { |
| 89 | + HWMON_T_INPUT | HWMON_T_LABEL, |
| 90 | + 0, |
| 91 | +}; |
| 92 | + |
| 93 | +static const struct hwmon_channel_info aq_hwmon_temp = { |
| 94 | + .type = hwmon_temp, |
| 95 | + .config = aq_hwmon_temp_config, |
| 96 | +}; |
| 97 | + |
| 98 | +static const struct hwmon_channel_info *aq_hwmon_info[] = { |
| 99 | + &aq_hwmon_temp, |
| 100 | + NULL, |
| 101 | +}; |
| 102 | + |
| 103 | +static const struct hwmon_chip_info aq_hwmon_chip_info = { |
| 104 | + .ops = &aq_hwmon_ops, |
| 105 | + .info = aq_hwmon_info, |
| 106 | +}; |
| 107 | + |
| 108 | +int aq_drvinfo_init(struct net_device *ndev) |
| 109 | +{ |
| 110 | + struct aq_nic_s *aq_nic = netdev_priv(ndev); |
| 111 | + struct device *dev = &aq_nic->pdev->dev; |
| 112 | + struct device *hwmon_dev; |
| 113 | + int err = 0; |
| 114 | + |
| 115 | + hwmon_dev = devm_hwmon_device_register_with_info(dev, |
| 116 | + ndev->name, |
| 117 | + aq_nic, |
| 118 | + &aq_hwmon_chip_info, |
| 119 | + NULL); |
| 120 | + |
| 121 | + if (IS_ERR(hwmon_dev)) |
| 122 | + err = PTR_ERR(hwmon_dev); |
| 123 | + |
| 124 | + return err; |
| 125 | +} |
0 commit comments