2 * ina3221.c - driver for TI INA3221
4 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software. you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysfs.h>
24 #include <linux/kobject.h>
25 #include <linux/hrtimer.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/mutex.h>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/err.h>
32 #include <linux/gpio.h>
33 #include <linux/device.h>
34 #include <linux/init.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/hwmon.h>
37 #include <linux/cpu.h>
38 #include <linux/cpufreq.h>
40 #include "linux/ina3221.h"
42 #define DRIVER_NAME "ina3221"
44 /* Set non-zero to enable debug prints */
45 #define INA3221_DEBUG_PRINTS 0
47 #if INA3221_DEBUG_PRINTS
48 #define DEBUG_INA3221(x) (printk x)
50 #define DEBUG_INA3221(x)
56 #define CPU_THRESHOLD 2
57 #define CPU_FREQ_THRESHOLD 102000
60 struct device *hwmon_dev;
61 struct i2c_client *client;
62 struct ina3221_platform_data *plat_data;
65 struct notifier_block nb;
66 struct notifier_block nb2;
67 int shutdown_complete;
71 static inline s32 shuntv_register_to_uv(u16 reg)
74 return (ret >> 3) * 40;
77 static inline u16 uv_to_shuntv_register(s32 uv)
82 static inline s32 busv_register_to_mv(u16 reg)
85 return (ret >> 3) * 8;
88 /* convert shunt voltage register value to current (in mA) */
89 static s32 shuntv_register_to_ma(u16 reg, s32 resistance)
93 uv = ((uv >> 3) * 40); /* LSB (4th bit) is 40uV */
94 /* calculate uv/resistance with rounding knowing that C99 truncates
97 ma = ((uv * 2 / resistance) + 1) / 2;
99 ma = ((uv * 2 / resistance) - 1) / 2;
104 __locked_set_crit_warn_register(struct i2c_client *client,
105 u32 index, u32 reg_addr)
107 struct ina3221_data *data = i2c_get_clientdata(client);
108 u32 shunt_volt_limit;
111 data->plat_data->crit_conf_limits[index] *
112 data->plat_data->shunt_resistor[index];
113 shunt_volt_limit = uv_to_shuntv_register(shunt_volt_limit);
115 DEBUG_INA3221(("Current = %d\n", shunt_volt_limit));
116 ret = i2c_smbus_write_word_data(client, reg_addr,
117 cpu_to_be16(shunt_volt_limit));
121 static s32 __locked_set_crit_warn_limits(struct i2c_client *client)
123 struct ina3221_data *data = i2c_get_clientdata(client);
128 if (!(data->plat_data->crit_conf_limits) ||
129 !(data->plat_data->warn_conf_limits))
132 for (i = 0; i < INA3221_NUMBER_OF_RAILS; i++) {
133 crit_reg_addr = INA3221_CRIT(i);
134 warn_reg_addr = INA3221_WARN(i);
135 if (data->plat_data->crit_conf_limits[i] != -1) {
136 ret = __locked_set_crit_warn_register(client,
142 if (data->plat_data->warn_conf_limits[i] != -1) {
143 ret = __locked_set_crit_warn_register(client,
152 static s32 __locked_power_down_ina3221(struct i2c_client *client)
155 struct ina3221_data *data = i2c_get_clientdata(client);
156 if (data->shutdown_complete)
158 ret = i2c_smbus_write_word_data(client, INA3221_CONFIG,
161 dev_err(&client->dev, "Power down failure status: 0x%x", ret);
165 static s32 __locked_power_up_ina3221(struct i2c_client *client, int config)
168 struct ina3221_data *data = i2c_get_clientdata(client);
169 if (data->shutdown_complete)
171 ret = i2c_smbus_write_word_data(client, INA3221_CONFIG,
172 cpu_to_be16(config));
174 dev_err(&client->dev,
175 "Config data write failed, error: 0x%x", ret);
179 static s32 __locked_start_conversion(struct i2c_client *client)
181 struct ina3221_data *data = i2c_get_clientdata(client);
183 int config = data->plat_data->trig_conf_data;
185 if (data->mode == TRIGGERED)
186 ret = __locked_power_up_ina3221(client, config);
191 static s32 __locked_end_conversion(struct i2c_client *client)
193 struct ina3221_data *data = i2c_get_clientdata(client);
196 if (data->mode == TRIGGERED)
197 ret = __locked_power_down_ina3221(client);
202 static s32 __locked_do_conversion(struct i2c_client *client, u16 *vsh,
205 struct ina3221_data *data = i2c_get_clientdata(client);
208 if (data->shutdown_complete)
211 ret = __locked_start_conversion(client);
216 ret = i2c_smbus_read_word_data(client, INA3221_SHUNT_VOL(ch));
219 *vsh = be16_to_cpu(ret);
223 ret = i2c_smbus_read_word_data(client, INA3221_BUS_VOL(ch));
226 *vbus = be16_to_cpu(ret);
229 ret = __locked_end_conversion(client);
235 static s32 show_mode(struct device *dev, struct device_attribute *attr,
238 struct i2c_client *client = to_i2c_client(dev);
239 struct ina3221_data *data = i2c_get_clientdata(client);
241 mutex_lock(&data->mutex);
242 ret = sprintf(buf, "%d\n", data->mode);
243 mutex_unlock(&data->mutex);
247 static s32 show_rail_name(struct device *dev,
248 struct device_attribute *devattr,
251 struct i2c_client *client = to_i2c_client(dev);
252 struct ina3221_data *data = i2c_get_clientdata(client);
253 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
254 u8 index = attr->index;
255 return sprintf(buf, "%s\n", data->plat_data->rail_name[index]);
258 static s32 show_voltage(struct device *dev,
259 struct device_attribute *devattr,
262 struct i2c_client *client = to_i2c_client(dev);
263 struct ina3221_data *data = i2c_get_clientdata(client);
264 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
270 mutex_lock(&data->mutex);
273 ret = __locked_do_conversion(client, NULL, &vbus, index);
277 voltage_mv = busv_register_to_mv(vbus);
279 DEBUG_INA3221(("%s volt = %d\n", __func__, voltage_mv));
280 mutex_unlock(&data->mutex);
281 return sprintf(buf, "%d mV\n", voltage_mv);
283 mutex_unlock(&data->mutex);
284 dev_err(dev, "%s: failed\n", __func__);
288 static s32 show_current(struct device *dev,
289 struct device_attribute *devattr,
292 struct i2c_client *client = to_i2c_client(dev);
293 struct ina3221_data *data = i2c_get_clientdata(client);
294 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
301 mutex_lock(&data->mutex);
304 ret = __locked_do_conversion(client, &vsh, NULL, index);
308 resistance = data->plat_data->shunt_resistor[index];
309 current_ma = shuntv_register_to_ma(vsh, resistance);
311 DEBUG_INA3221(("%s current = %d\n", __func__, current_ma));
312 mutex_unlock(&data->mutex);
313 return sprintf(buf, "%d mA\n", current_ma);
315 mutex_unlock(&data->mutex);
316 dev_err(dev, "%s: failed\n", __func__);
320 static s32 show_current2(struct device *dev,
321 struct device_attribute *devattr,
324 struct i2c_client *client = to_i2c_client(dev);
325 struct ina3221_data *data = i2c_get_clientdata(client);
326 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
333 mutex_lock(&data->mutex);
334 if (data->shutdown_complete) {
339 /*return 0 if INA is off*/
340 if (data->mode == TRIGGERED) {
341 mutex_unlock(&data->mutex);
342 return sprintf(buf, "%d mA\n", 0);
347 ret = __locked_do_conversion(client, &vsh, NULL, index);
351 resistance = data->plat_data->shunt_resistor[index];
352 current_ma = shuntv_register_to_ma(vsh, resistance);
354 DEBUG_INA3221(("%s current = %d\n", __func__, current_ma));
355 mutex_unlock(&data->mutex);
356 return sprintf(buf, "%d mA\n", current_ma);
358 mutex_unlock(&data->mutex);
359 dev_err(dev, "%s: failed\n", __func__);
363 static s32 show_power(struct device *dev,
364 struct device_attribute *devattr,
367 struct i2c_client *client = to_i2c_client(dev);
368 struct ina3221_data *data = i2c_get_clientdata(client);
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
373 s32 current_ma, voltage_mv;
377 mutex_lock(&data->mutex);
380 ret = __locked_do_conversion(client, &vsh, &vbus, index);
384 resistance = data->plat_data->shunt_resistor[index];
385 current_ma = shuntv_register_to_ma(vsh, resistance);
386 voltage_mv = busv_register_to_mv(vbus);
388 power_mw = voltage_mv * current_ma / 1000;
390 DEBUG_INA3221(("%s power = %d\n", __func__, power_mw));
391 mutex_unlock(&data->mutex);
392 return sprintf(buf, "%d mW\n", power_mw);
394 mutex_unlock(&data->mutex);
395 dev_err(dev, "%s: failed\n", __func__);
399 static s32 show_power2(struct device *dev,
400 struct device_attribute *devattr,
403 struct i2c_client *client = to_i2c_client(dev);
404 struct ina3221_data *data = i2c_get_clientdata(client);
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 s32 current_ma, voltage_mv;
413 mutex_lock(&data->mutex);
414 if (data->shutdown_complete) {
419 /*return 0 if INA is off*/
420 if (data->mode == TRIGGERED) {
421 mutex_unlock(&data->mutex);
422 return sprintf(buf, "%d mW\n", 0);
426 ret = __locked_do_conversion(client, &vsh, &vbus, index);
430 resistance = data->plat_data->shunt_resistor[index];
431 current_ma = shuntv_register_to_ma(vsh, resistance);
432 voltage_mv = busv_register_to_mv(vbus);
434 power_mw = voltage_mv * current_ma / 1000;
436 DEBUG_INA3221(("%s power = %d\n", __func__, power_mw));
437 mutex_unlock(&data->mutex);
438 return sprintf(buf, "%d mW\n", power_mw);
440 mutex_unlock(&data->mutex);
441 dev_err(dev, "%s: failed\n", __func__);
445 static int __locked_ina3221_switch(struct ina3221_data *data,
446 struct i2c_client *client,
447 int cpus, int cpufreq)
450 if ((data->mode == TRIGGERED) &&
451 ((cpus >= CPU_THRESHOLD) ||
452 (cpufreq >= CPU_FREQ_THRESHOLD))) {
454 * Turn INA on when cpu frequency crosses threshold or number of cpus
457 DEBUG_INA3221(("Turning on ina3221, cpus:%d, cpufreq:%d\n",
459 ret = __locked_power_up_ina3221(client,
460 data->plat_data->cont_conf_data);
462 dev_err(&client->dev,
463 "INA can't be turned on: 0x%x\n", ret);
466 data->mode = CONTINUOUS;
468 } else if ((data->mode == CONTINUOUS) &&
469 (cpus < CPU_THRESHOLD) &&
470 (cpufreq < CPU_FREQ_THRESHOLD)) {
472 * Turn off ina when number of cpu cores on are below threshold
473 * and cpu frequency are below threshold
475 DEBUG_INA3221(("Turning off ina3221, cpus:%d, cpufreq:%d\n",
477 ret = __locked_power_down_ina3221(client);
479 dev_err(&client->dev,
480 "INA can't be turned off: 0x%x\n", ret);
483 data->mode = TRIGGERED;
490 static int ina3221_cpufreq_notify(struct notifier_block *nb,
497 struct ina3221_data *data = container_of(nb, struct ina3221_data, nb2);
498 struct i2c_client *client = data->client;
499 if (event == CPUFREQ_POSTCHANGE) {
500 mutex_lock(&data->mutex);
501 if (data->is_suspended) {
502 mutex_unlock(&data->mutex);
505 cpufreq = ((struct cpufreq_freqs *)hcpu)->new;
506 cpus = num_online_cpus();
507 DEBUG_INA3221(("***INA3221 CPUfreq notified freq:%d cpus:%d\n",
509 ret = __locked_ina3221_switch(data, client, cpus, cpufreq);
512 mutex_unlock(&data->mutex);
517 mutex_unlock(&data->mutex);
518 dev_err(&client->dev, "INA can't be turned off/on: 0x%x\n", ret);
522 static int ina3221_hotplug_notify(struct notifier_block *nb,
526 struct ina3221_data *data = container_of(nb, struct ina3221_data,
528 struct i2c_client *client = data->client;
532 if (event == CPU_ONLINE || event == CPU_DEAD) {
533 mutex_lock(&data->mutex);
534 cpufreq = cpufreq_quick_get(0);
535 cpus = num_online_cpus();
536 DEBUG_INA3221(("INA3221 hotplug notified cpufreq:%d cpus:%d\n",
538 ret = __locked_ina3221_switch(data, client, cpus, cpufreq);
541 mutex_unlock(&data->mutex);
546 mutex_unlock(&data->mutex);
547 dev_err(&client->dev, "INA can't be turned off/on: 0x%x\n", ret);
551 static s32 set_crit(struct device *dev,
552 struct device_attribute *devattr,
553 const char *buf, size_t count)
555 struct i2c_client *client = to_i2c_client(dev);
556 struct ina3221_data *data = i2c_get_clientdata(client);
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 s32 retval, crit_reg_addr;
562 mutex_lock(&data->mutex);
563 if (data->shutdown_complete) {
569 if (kstrtol(buf, 10, &curr_limit) < 0) {
574 data->plat_data->crit_conf_limits[index] = curr_limit;
575 crit_reg_addr = INA3221_CRIT(index);
576 retval = __locked_set_crit_warn_register(client, index, crit_reg_addr);
579 mutex_unlock(&data->mutex);
585 static s32 show_crit(struct device *dev,
586 struct device_attribute *devattr,
589 struct i2c_client *client = to_i2c_client(dev);
590 struct ina3221_data *data = i2c_get_clientdata(client);
591 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
592 u8 index, crit_reg_addr;
596 mutex_lock(&data->mutex);
597 if (data->shutdown_complete) {
603 crit_reg_addr = INA3221_CRIT(index);
605 /* getting voltage readings in micro volts*/
606 ret = i2c_smbus_read_word_data(client, crit_reg_addr);
610 resistance = data->plat_data->shunt_resistor[index];
611 current_ma = shuntv_register_to_ma(be16_to_cpu(ret), resistance);
613 DEBUG_INA3221(("Ina3221 crit current in mA: %d\n", current_ma));
615 mutex_unlock(&data->mutex);
616 return sprintf(buf, "%d mA\n", current_ma);
618 mutex_unlock(&data->mutex);
619 dev_err(dev, "%s: failed\n", __func__);
623 static struct sensor_device_attribute ina3221[] = {
624 SENSOR_ATTR(rail_name_0, S_IRUGO, show_rail_name, NULL, 0),
625 SENSOR_ATTR(in1_input_0, S_IRUGO, show_voltage, NULL, 0),
626 SENSOR_ATTR(curr1_input_0, S_IRUGO, show_current, NULL, 0),
627 SENSOR_ATTR(curr2_input_0, S_IRUGO, show_current2, NULL, 0),
628 SENSOR_ATTR(power1_input_0, S_IRUGO, show_power, NULL, 0),
629 SENSOR_ATTR(power2_input_0, S_IRUGO, show_power2, NULL, 0),
630 SENSOR_ATTR(rail_name_1, S_IRUGO, show_rail_name, NULL, 1),
631 SENSOR_ATTR(in1_input_1, S_IRUGO, show_voltage, NULL, 1),
632 SENSOR_ATTR(curr1_input_1, S_IRUGO, show_current, NULL, 1),
633 SENSOR_ATTR(curr2_input_1, S_IRUGO, show_current2, NULL, 1),
634 SENSOR_ATTR(power1_input_1, S_IRUGO, show_power, NULL, 1),
635 SENSOR_ATTR(power2_input_1, S_IRUGO, show_power2, NULL, 1),
636 SENSOR_ATTR(rail_name_2, S_IRUGO, show_rail_name, NULL, 2),
637 SENSOR_ATTR(in1_input_2, S_IRUGO, show_voltage, NULL, 2),
638 SENSOR_ATTR(curr1_input_2, S_IRUGO, show_current, NULL, 2),
639 SENSOR_ATTR(curr2_input_2, S_IRUGO, show_current2, NULL, 2),
640 SENSOR_ATTR(power1_input_2, S_IRUGO, show_power, NULL, 2),
641 SENSOR_ATTR(power2_input_2, S_IRUGO, show_power2, NULL, 2),
642 SENSOR_ATTR(running_mode, S_IRUGO, show_mode, NULL, 0),
643 SENSOR_ATTR(crit_cur_limit_0, S_IWUSR|S_IRUGO, show_crit, set_crit, 0),
644 SENSOR_ATTR(crit_cur_limit_1, S_IWUSR|S_IRUGO, show_crit, set_crit, 1),
645 SENSOR_ATTR(crit_cur_limit_2, S_IWUSR|S_IRUGO, show_crit, set_crit, 2),
647 * running_mode = 0 ---> Triggered mode
648 * running_mode > 0 ---> Continuous mode
652 static int ina3221_probe(struct i2c_client *client,
653 const struct i2c_device_id *id)
655 struct ina3221_data *data;
658 data = devm_kzalloc(&client->dev, sizeof(struct ina3221_data),
664 i2c_set_clientdata(client, data);
665 data->plat_data = client->dev.platform_data;
666 mutex_init(&data->mutex);
668 data->mode = TRIGGERED;
669 data->shutdown_complete = 0;
670 data->is_suspended = 0;
672 ret = i2c_smbus_write_word_data(client, INA3221_CONFIG,
673 __constant_cpu_to_be16((INA3221_RESET)));
675 dev_err(&client->dev, "ina3221 reset failure status: 0x%x\n",
680 for (i = 0; i < ARRAY_SIZE(ina3221); i++) {
681 ret = device_create_file(&client->dev, &ina3221[i].dev_attr);
683 dev_err(&client->dev, "device_create_file failed.\n");
687 data->client = client;
688 data->nb.notifier_call = ina3221_hotplug_notify;
689 data->nb2.notifier_call = ina3221_cpufreq_notify;
690 register_hotcpu_notifier(&(data->nb));
691 cpufreq_register_notifier(&(data->nb2), CPUFREQ_TRANSITION_NOTIFIER);
692 data->hwmon_dev = hwmon_device_register(&client->dev);
693 if (IS_ERR(data->hwmon_dev)) {
694 ret = PTR_ERR(data->hwmon_dev);
698 ret = __locked_set_crit_warn_limits(client);
700 dev_info(&client->dev, "Not able to set warn and crit limits!\n");
701 /*Not an error condition, could let the probe continue*/
704 /* set ina3221 to power down mode */
705 ret = __locked_power_down_ina3221(client);
713 device_remove_file(&client->dev, &ina3221[i].dev_attr);
715 devm_kfree(&client->dev, data);
720 static int ina3221_remove(struct i2c_client *client)
723 struct ina3221_data *data = i2c_get_clientdata(client);
724 mutex_lock(&data->mutex);
725 __locked_power_down_ina3221(client);
726 hwmon_device_unregister(data->hwmon_dev);
727 mutex_unlock(&data->mutex);
728 unregister_hotcpu_notifier(&(data->nb));
729 cpufreq_unregister_notifier(&(data->nb2), CPUFREQ_TRANSITION_NOTIFIER);
730 for (i = 0; i < ARRAY_SIZE(ina3221); i++)
731 device_remove_file(&client->dev, &ina3221[i].dev_attr);
735 static void ina3221_shutdown(struct i2c_client *client)
737 struct ina3221_data *data = i2c_get_clientdata(client);
738 mutex_lock(&data->mutex);
739 __locked_power_down_ina3221(client);
740 data->shutdown_complete = 1;
741 mutex_unlock(&data->mutex);
744 #ifdef CONFIG_PM_SLEEP
745 static int ina3221_suspend(struct device *dev)
747 struct i2c_client *client = to_i2c_client(dev);
748 struct ina3221_data *data = i2c_get_clientdata(client);
750 mutex_lock(&data->mutex);
751 ret = __locked_power_down_ina3221(client);
753 dev_err(&client->dev,
754 "INA can't be turned off: 0x%x\n", ret);
757 data->mode = TRIGGERED;
758 data->is_suspended = 1;
760 mutex_unlock(&data->mutex);
764 static int ina3221_resume(struct device *dev)
766 int cpufreq, cpus, ret;
767 struct i2c_client *client = to_i2c_client(dev);
768 struct ina3221_data *data = i2c_get_clientdata(client);
769 mutex_lock(&data->mutex);
770 cpufreq = cpufreq_quick_get(0);
771 cpus = num_online_cpus();
772 ret = __locked_ina3221_switch(data, client, cpus, cpufreq);
774 dev_err(&client->dev,
775 "INA can't be turned off/on: 0x%x\n", ret);
777 data->is_suspended = 0;
778 mutex_unlock(&data->mutex);
783 static const struct i2c_device_id ina3221_id[] = {
787 MODULE_DEVICE_TABLE(i2c, ina3221_id);
789 #ifdef CONFIG_PM_SLEEP
790 static const struct dev_pm_ops ina3221_pm_ops = {
791 SET_SYSTEM_SLEEP_PM_OPS(ina3221_suspend,
796 static struct i2c_driver ina3221_driver = {
797 .class = I2C_CLASS_HWMON,
800 .owner = THIS_MODULE,
801 #ifdef CONFIG_PM_SLEEP
802 .pm = &ina3221_pm_ops,
805 .probe = ina3221_probe,
806 .remove = ina3221_remove,
807 .shutdown = ina3221_shutdown,
808 .id_table = ina3221_id,
811 static int __init ina3221_init(void)
813 return i2c_add_driver(&ina3221_driver);
816 static void __exit ina3221_exit(void)
818 i2c_del_driver(&ina3221_driver);
821 module_init(ina3221_init);
822 module_exit(ina3221_exit);
823 MODULE_LICENSE("GPL");