|
| 1 | +#include <linux/module.h> |
| 2 | +#include <linux/jiffies.h> |
| 3 | +#include <linux/i2c.h> |
| 4 | +#include <linux/hwmon.h> |
| 5 | +#include <linux/hwmon-sysfs.h> |
| 6 | +#include <linux/err.h> |
| 7 | +#include <linux/delay.h> |
| 8 | +#include <linux/mutex.h> |
| 9 | +#include <linux/sysfs.h> |
| 10 | +#include <linux/slab.h> |
| 11 | +#include <linux/dmi.h> |
| 12 | +#include "pddf_psu_defs.h" |
| 13 | + |
| 14 | +ssize_t pddf_show_custom_psu_v_out(struct device *dev, struct device_attribute *da, char *buf); |
| 15 | +extern PSU_SYSFS_ATTR_DATA access_psu_v_out; |
| 16 | + |
| 17 | +static int two_complement_to_int(u16 data, u8 valid_bit, int mask) |
| 18 | +{ |
| 19 | + u16 valid_data = data & mask; |
| 20 | + bool is_negative = valid_data >> (valid_bit - 1); |
| 21 | + |
| 22 | + return is_negative ? (-(((~valid_data) & mask) + 1)) : valid_data; |
| 23 | +} |
| 24 | + |
| 25 | +static u8 psu_get_vout_mode(struct i2c_client *client) |
| 26 | +{ |
| 27 | + u8 status = 0, retry = 10; |
| 28 | + uint8_t offset = 0x20; // VOUT_MODE |
| 29 | + |
| 30 | + while (retry) { |
| 31 | + status = i2c_smbus_read_byte_data((struct i2c_client *)client, offset); |
| 32 | + if (unlikely(status < 0)) { |
| 33 | + msleep(60); |
| 34 | + retry--; |
| 35 | + continue; |
| 36 | + } |
| 37 | + break; |
| 38 | + } |
| 39 | + |
| 40 | + if (status < 0) |
| 41 | + { |
| 42 | + printk(KERN_ERR "%s: Get PSU Vout mode failed\n", __func__); |
| 43 | + return 0; |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + /*printk(KERN_ERR "%s: vout_mode reg value 0x%x\n", __func__, status);*/ |
| 48 | + return status; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +static u16 psu_get_v_out(struct i2c_client *client) |
| 53 | +{ |
| 54 | + u16 status = 0, retry = 10; |
| 55 | + uint8_t offset = 0x8b; // READ_VOUT |
| 56 | + |
| 57 | + while (retry) { |
| 58 | + status = i2c_smbus_read_word_data((struct i2c_client *)client, offset); |
| 59 | + if (unlikely(status < 0)) { |
| 60 | + msleep(60); |
| 61 | + retry--; |
| 62 | + continue; |
| 63 | + } |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + if (status < 0) |
| 68 | + { |
| 69 | + printk(KERN_ERR "%s: Get PSU Vout failed\n", __func__); |
| 70 | + return 0; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + /*printk(KERN_ERR "%s: vout reg value 0x%x\n", __func__, status);*/ |
| 75 | + return status; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +ssize_t pddf_show_custom_psu_v_out(struct device *dev, struct device_attribute *da, char *buf) |
| 80 | +{ |
| 81 | + struct i2c_client *client = to_i2c_client(dev); |
| 82 | + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 83 | + int exponent, mantissa; |
| 84 | + int multiplier = 1000; |
| 85 | + |
| 86 | + u16 value = psu_get_v_out(client); |
| 87 | + u8 vout_mode = psu_get_vout_mode(client); |
| 88 | + |
| 89 | + exponent = two_complement_to_int(vout_mode, 5, 0x1f); |
| 90 | + mantissa = value; |
| 91 | + if (exponent >= 0) |
| 92 | + return sprintf(buf, "%d\n", (mantissa << exponent) * multiplier); |
| 93 | + else |
| 94 | + return sprintf(buf, "%d\n", (mantissa * multiplier) / (1 << -exponent)); |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +static int __init pddf_custom_psu_init(void) |
| 100 | +{ |
| 101 | + access_psu_v_out.show = pddf_show_custom_psu_v_out; |
| 102 | + access_psu_v_out.do_get = NULL; |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +static void __exit pddf_custom_psu_exit(void) |
| 107 | +{ |
| 108 | + return; |
| 109 | +} |
| 110 | + |
| 111 | +MODULE_AUTHOR("Broadcom"); |
| 112 | +MODULE_DESCRIPTION("pddf custom psu api"); |
| 113 | +MODULE_LICENSE("GPL"); |
| 114 | + |
| 115 | +module_init(pddf_custom_psu_init); |
| 116 | +module_exit(pddf_custom_psu_exit); |
| 117 | + |
0 commit comments