]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/hwmon/ina3221.c
97ac696cb08e83612935d8e3610555278a613bbd
[linux-3.10.git] / drivers / hwmon / ina3221.c
1 /*
2  * ina3221.c - driver for TI INA3221
3  *
4  * Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
5  *
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.
9  *
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
13  * more details.
14  *
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.
18  */
19
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>
39
40 #include "linux/ina3221.h"
41
42 #define DRIVER_NAME "ina3221"
43
44 /* Set non-zero to enable debug prints */
45 #define INA3221_DEBUG_PRINTS 0
46
47 #if INA3221_DEBUG_PRINTS
48 #define DEBUG_INA3221(x) (printk x)
49 #else
50 #define DEBUG_INA3221(x)
51 #endif
52
53 #define TRIGGERED 0
54 #define CONTINUOUS 1
55
56 #define CPU_THRESHOLD 2
57 #define CPU_FREQ_THRESHOLD 102000
58
59 struct ina3221_data {
60         struct device *hwmon_dev;
61         struct i2c_client *client;
62         struct ina3221_platform_data *plat_data;
63         struct mutex mutex;
64         u8 mode;
65         struct notifier_block nb;
66         struct notifier_block nb2;
67         int shutdown_complete;
68         int is_suspended;
69 };
70
71 static inline s32 shuntv_register_to_uv(u16 reg)
72 {
73         s32 ret = (s16)reg;
74         return (ret >> 3) * 40;
75 }
76
77 static inline u16 uv_to_shuntv_register(s32 uv)
78 {
79         return (u16)(uv/5);
80 }
81
82 static inline s32 busv_register_to_mv(u16 reg)
83 {
84         s32 ret = (s16)reg;
85         return (ret >> 3) * 8;
86 }
87
88 /* convert shunt voltage register value to current (in mA) */
89 static s32 shuntv_register_to_ma(u16 reg, s32 resistance)
90 {
91         s32 uv, ma;
92         uv = (s16)reg;
93         uv = ((uv >> 3) * 40); /* LSB (4th bit) is 40uV */
94         /* calculate uv/resistance with rounding knowing that C99 truncates
95          * towards zero */
96         if (uv > 0)
97                 ma = ((uv * 2 / resistance) + 1) / 2;
98         else
99                 ma = ((uv * 2 / resistance) - 1) / 2;
100         return ma;
101 }
102
103 static s32
104 __locked_set_crit_warn_register(struct i2c_client *client,
105 u32 index, u32 reg_addr)
106 {
107         struct ina3221_data *data = i2c_get_clientdata(client);
108         u32 shunt_volt_limit;
109         s32 ret;
110         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);
114
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));
118         return ret;
119 }
120
121 static s32 __locked_set_crit_warn_limits(struct i2c_client *client)
122 {
123         struct ina3221_data *data = i2c_get_clientdata(client);
124         u32 i;
125         u32 crit_reg_addr;
126         u32 warn_reg_addr;
127         s32 ret = 0;
128         if (!(data->plat_data->crit_conf_limits) ||
129                 !(data->plat_data->warn_conf_limits))
130                 return -EINVAL;
131
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,
137                                                         i, crit_reg_addr);
138                         if (ret < 0)
139                                 break;
140                 }
141
142                 if (data->plat_data->warn_conf_limits[i] != -1) {
143                         ret = __locked_set_crit_warn_register(client,
144                                                         i, warn_reg_addr);
145                         if (ret < 0)
146                                 break;
147                 }
148         }
149         return ret;
150 }
151
152 static s32 __locked_power_down_ina3221(struct i2c_client *client)
153 {
154         s32 ret;
155         struct ina3221_data *data = i2c_get_clientdata(client);
156         if (data->shutdown_complete)
157                 return -ENODEV;
158         ret = i2c_smbus_write_word_data(client, INA3221_CONFIG,
159                 INA3221_POWER_DOWN);
160         if (ret < 0)
161                 dev_err(&client->dev, "Power down failure status: 0x%x", ret);
162         return ret;
163 }
164
165 static s32 __locked_power_up_ina3221(struct i2c_client *client, int config)
166 {
167         s32 ret;
168         struct ina3221_data *data = i2c_get_clientdata(client);
169         if (data->shutdown_complete)
170                 return -ENODEV;
171         ret = i2c_smbus_write_word_data(client, INA3221_CONFIG,
172                                         cpu_to_be16(config));
173         if (ret < 0)
174                 dev_err(&client->dev,
175                         "Config data write failed, error: 0x%x", ret);
176         return ret;
177 }
178
179 static s32 __locked_start_conversion(struct i2c_client *client)
180 {
181         struct ina3221_data *data = i2c_get_clientdata(client);
182         s32 ret = 0;
183         int config = data->plat_data->trig_conf_data;
184
185         if (data->mode == TRIGGERED)
186                 ret = __locked_power_up_ina3221(client, config);
187
188         return ret;
189 }
190
191 static s32 __locked_end_conversion(struct i2c_client *client)
192 {
193         struct ina3221_data *data = i2c_get_clientdata(client);
194         s32 ret = 0;
195
196         if (data->mode == TRIGGERED)
197                 ret = __locked_power_down_ina3221(client);
198
199         return ret;
200 }
201
202 static s32 __locked_do_conversion(struct i2c_client *client, u16 *vsh,
203                                   u16 *vbus, int ch)
204 {
205         struct ina3221_data *data = i2c_get_clientdata(client);
206         s32 ret;
207
208         if (data->shutdown_complete)
209                 return -ENODEV;
210
211         ret = __locked_start_conversion(client);
212         if (ret < 0)
213                 return ret;
214
215         if (vsh) {
216                 ret = i2c_smbus_read_word_data(client, INA3221_SHUNT_VOL(ch));
217                 if (ret < 0)
218                         return ret;
219                 *vsh = be16_to_cpu(ret);
220         }
221
222         if (vbus) {
223                 ret = i2c_smbus_read_word_data(client, INA3221_BUS_VOL(ch));
224                 if (ret < 0)
225                         return ret;
226                 *vbus = be16_to_cpu(ret);
227         }
228
229         ret = __locked_end_conversion(client);
230
231         return ret;
232 }
233
234
235 static s32 show_mode(struct device *dev, struct device_attribute *attr,
236                                                                 char *buf)
237 {
238         struct i2c_client *client = to_i2c_client(dev);
239         struct ina3221_data *data = i2c_get_clientdata(client);
240         int ret;
241         mutex_lock(&data->mutex);
242         ret = sprintf(buf, "%d\n", data->mode);
243         mutex_unlock(&data->mutex);
244         return ret;
245 }
246
247 static s32 show_rail_name(struct device *dev,
248                 struct device_attribute *devattr,
249                 char *buf)
250 {
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]);
256 }
257
258 static s32 show_voltage(struct device *dev,
259                 struct device_attribute *devattr,
260                 char *buf)
261 {
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);
265         u8 index;
266         u16 vbus;
267         s32 ret;
268         s32 voltage_mv;
269
270         mutex_lock(&data->mutex);
271         index = attr->index;
272
273         ret = __locked_do_conversion(client, NULL, &vbus, index);
274         if (ret < 0)
275                 goto error;
276
277         voltage_mv = busv_register_to_mv(vbus);
278
279         DEBUG_INA3221(("%s volt = %d\n", __func__, voltage_mv));
280         mutex_unlock(&data->mutex);
281         return sprintf(buf, "%d mV\n", voltage_mv);
282 error:
283         mutex_unlock(&data->mutex);
284         dev_err(dev, "%s: failed\n", __func__);
285         return ret;
286 }
287
288 static s32 show_current(struct device *dev,
289                 struct device_attribute *devattr,
290                 char *buf)
291 {
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);
295         u8 index;
296         u16 vsh;
297         s32 ret;
298         s32 current_ma;
299         s32 resistance;
300
301         mutex_lock(&data->mutex);
302         index = attr->index;
303
304         ret = __locked_do_conversion(client, &vsh, NULL, index);
305         if (ret < 0)
306                 goto error;
307
308         resistance = data->plat_data->shunt_resistor[index];
309         current_ma = shuntv_register_to_ma(vsh, resistance);
310
311         DEBUG_INA3221(("%s current = %d\n", __func__, current_ma));
312         mutex_unlock(&data->mutex);
313         return sprintf(buf, "%d mA\n", current_ma);
314 error:
315         mutex_unlock(&data->mutex);
316         dev_err(dev, "%s: failed\n", __func__);
317         return ret;
318 }
319
320 static s32 show_current2(struct device *dev,
321                 struct device_attribute *devattr,
322                 char *buf)
323 {
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);
327         u8 index;
328         u16 vsh;
329         s32 ret;
330         s32 current_ma;
331         s32 resistance;
332
333         mutex_lock(&data->mutex);
334         if (data->shutdown_complete) {
335                 ret = -ENODEV;
336                 goto error;
337         }
338
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);
343         }
344
345         index = attr->index;
346
347         ret = __locked_do_conversion(client, &vsh, NULL, index);
348         if (ret < 0)
349                 goto error;
350
351         resistance = data->plat_data->shunt_resistor[index];
352         current_ma = shuntv_register_to_ma(vsh, resistance);
353
354         DEBUG_INA3221(("%s current = %d\n", __func__, current_ma));
355         mutex_unlock(&data->mutex);
356         return sprintf(buf, "%d mA\n", current_ma);
357 error:
358         mutex_unlock(&data->mutex);
359         dev_err(dev, "%s: failed\n", __func__);
360         return ret;
361 }
362
363 static s32 show_power(struct device *dev,
364                 struct device_attribute *devattr,
365                 char *buf)
366 {
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);
370         u8 index;
371         u16 vsh, vbus;
372         s32 ret;
373         s32 current_ma, voltage_mv;
374         s32 resistance;
375         s32 power_mw;
376
377         mutex_lock(&data->mutex);
378         index = attr->index;
379
380         ret = __locked_do_conversion(client, &vsh, &vbus, index);
381         if (ret < 0)
382                 goto error;
383
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);
387
388         power_mw = voltage_mv * current_ma / 1000;
389
390         DEBUG_INA3221(("%s power = %d\n", __func__, power_mw));
391         mutex_unlock(&data->mutex);
392         return sprintf(buf, "%d mW\n", power_mw);
393 error:
394         mutex_unlock(&data->mutex);
395         dev_err(dev, "%s: failed\n", __func__);
396         return ret;
397 }
398
399 static s32 show_power2(struct device *dev,
400                 struct device_attribute *devattr,
401                 char *buf)
402 {
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);
406         u8 index;
407         u16 vsh, vbus;
408         s32 ret;
409         s32 current_ma, voltage_mv;
410         s32 resistance;
411         s32 power_mw;
412
413         mutex_lock(&data->mutex);
414         if (data->shutdown_complete) {
415                 ret = -ENODEV;
416                 goto error;
417         }
418
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);
423         }
424         index = attr->index;
425
426         ret = __locked_do_conversion(client, &vsh, &vbus, index);
427         if (ret < 0)
428                 goto error;
429
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);
433
434         power_mw = voltage_mv * current_ma / 1000;
435
436         DEBUG_INA3221(("%s power = %d\n", __func__, power_mw));
437         mutex_unlock(&data->mutex);
438         return sprintf(buf, "%d mW\n", power_mw);
439 error:
440         mutex_unlock(&data->mutex);
441         dev_err(dev, "%s: failed\n", __func__);
442         return ret;
443 }
444
445 static int __locked_ina3221_switch(struct ina3221_data *data,
446                 struct i2c_client *client,
447                 int cpus, int cpufreq)
448 {
449         int ret = 0;
450         if ((data->mode == TRIGGERED) &&
451                 ((cpus >= CPU_THRESHOLD) ||
452                 (cpufreq >= CPU_FREQ_THRESHOLD))) {
453                 /**
454                  * Turn INA on when cpu frequency crosses threshold or number of cpus
455                  * crosses threshold
456                  */
457                 DEBUG_INA3221(("Turning on ina3221, cpus:%d, cpufreq:%d\n",
458                                         cpus, cpufreq));
459                 ret = __locked_power_up_ina3221(client,
460                                 data->plat_data->cont_conf_data);
461                 if (ret < 0) {
462                         dev_err(&client->dev,
463                                         "INA can't be turned on: 0x%x\n", ret);
464                         return ret;
465                 }
466                 data->mode = CONTINUOUS;
467                 return ret;
468         } else if ((data->mode == CONTINUOUS) &&
469                          (cpus < CPU_THRESHOLD) &&
470                         (cpufreq < CPU_FREQ_THRESHOLD)) {
471                 /*
472                  * Turn off ina when number of cpu cores on are below threshold
473                  * and cpu frequency are below threshold
474                  */
475                 DEBUG_INA3221(("Turning off ina3221, cpus:%d, cpufreq:%d\n",
476                                 cpus, cpufreq));
477                 ret = __locked_power_down_ina3221(client);
478                 if (ret < 0) {
479                         dev_err(&client->dev,
480                                         "INA can't be turned off: 0x%x\n", ret);
481                         return ret;
482                 }
483                 data->mode = TRIGGERED;
484                 return ret;
485         } else {
486                 return ret;
487         }
488 }
489
490 static int ina3221_cpufreq_notify(struct notifier_block *nb,
491                                         unsigned long event,
492                                         void *hcpu)
493 {
494         int ret = 0;
495         int cpufreq;
496         int cpus;
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);
503                         return 0;
504                 }
505                 cpufreq = ((struct cpufreq_freqs *)hcpu)->new;
506                 cpus = num_online_cpus();
507                 DEBUG_INA3221(("***INA3221 CPUfreq notified freq:%d cpus:%d\n",
508                                                 cpufreq, cpus));
509                 ret = __locked_ina3221_switch(data, client, cpus, cpufreq);
510                 if (ret < 0)
511                         goto error;
512                 mutex_unlock(&data->mutex);
513                 return 0;
514         } else
515                 return 0;
516 error:
517         mutex_unlock(&data->mutex);
518         dev_err(&client->dev, "INA can't be turned off/on: 0x%x\n", ret);
519         return 0;
520 }
521
522 static int ina3221_hotplug_notify(struct notifier_block *nb,
523                                         unsigned long event,
524                                         void *hcpu)
525 {
526         struct ina3221_data *data = container_of(nb, struct ina3221_data,
527                                                 nb);
528         struct i2c_client *client = data->client;
529         int cpus;
530         int ret = 0;
531         int cpufreq = 0;
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",
537                                 cpufreq, cpus));
538                 ret = __locked_ina3221_switch(data, client, cpus, cpufreq);
539                 if (ret < 0)
540                         goto error;
541                 mutex_unlock(&data->mutex);
542                 return 0;
543         } else
544                 return 0;
545 error:
546         mutex_unlock(&data->mutex);
547         dev_err(&client->dev, "INA can't be turned off/on: 0x%x\n", ret);
548         return 0;
549 }
550
551 static s32 set_crit(struct device *dev,
552                         struct device_attribute *devattr,
553                         const char *buf, size_t count)
554 {
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;
559         u32 index = 0;
560         long int curr_limit;
561
562         mutex_lock(&data->mutex);
563         if (data->shutdown_complete) {
564                 retval = -ENODEV;
565                 goto error;
566         }
567         index = attr->index;
568
569         if (kstrtol(buf, 10, &curr_limit) < 0) {
570                 retval = -EINVAL;
571                 goto error;
572         }
573
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);
577
578 error:
579         mutex_unlock(&data->mutex);
580         if (retval >= 0)
581                 return count;
582         return retval;
583 }
584
585 static s32 show_crit(struct device *dev,
586                 struct device_attribute *devattr,
587                 char *buf)
588 {
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;
593         s32 ret, current_ma;
594         s32 resistance;
595
596         mutex_lock(&data->mutex);
597         if (data->shutdown_complete) {
598                 ret = -ENODEV;
599                 goto error;
600         }
601         index = attr->index;
602
603         crit_reg_addr = INA3221_CRIT(index);
604
605         /* getting voltage readings in micro volts*/
606         ret = i2c_smbus_read_word_data(client, crit_reg_addr);
607         if (ret < 0)
608                 goto error;
609
610         resistance = data->plat_data->shunt_resistor[index];
611         current_ma = shuntv_register_to_ma(be16_to_cpu(ret), resistance);
612
613         DEBUG_INA3221(("Ina3221 crit current in mA: %d\n", current_ma));
614
615         mutex_unlock(&data->mutex);
616         return sprintf(buf, "%d mA\n", current_ma);
617 error:
618         mutex_unlock(&data->mutex);
619         dev_err(dev, "%s: failed\n", __func__);
620         return ret;
621 }
622
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),
646 /* mode setting :
647  * running_mode = 0 ---> Triggered mode
648  * running_mode > 0 ---> Continuous mode
649  */
650 };
651
652 static int ina3221_probe(struct i2c_client *client,
653                         const struct i2c_device_id *id)
654 {
655         struct ina3221_data *data;
656         int ret, i;
657
658         data = devm_kzalloc(&client->dev, sizeof(struct ina3221_data),
659                                                 GFP_KERNEL);
660         if (!data) {
661                 ret = -ENOMEM;
662                 goto exit;
663         }
664         i2c_set_clientdata(client, data);
665         data->plat_data = client->dev.platform_data;
666         mutex_init(&data->mutex);
667
668         data->mode = TRIGGERED;
669         data->shutdown_complete = 0;
670         data->is_suspended = 0;
671         /* reset ina3221 */
672         ret = i2c_smbus_write_word_data(client, INA3221_CONFIG,
673                 __constant_cpu_to_be16((INA3221_RESET)));
674         if (ret < 0) {
675                 dev_err(&client->dev, "ina3221 reset failure status: 0x%x\n",
676                         ret);
677                 goto exit_free;
678         }
679
680         for (i = 0; i < ARRAY_SIZE(ina3221); i++) {
681                 ret = device_create_file(&client->dev, &ina3221[i].dev_attr);
682                 if (ret) {
683                         dev_err(&client->dev, "device_create_file failed.\n");
684                         goto exit_remove;
685                 }
686         }
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);
695                 goto exit_remove;
696         }
697
698         ret = __locked_set_crit_warn_limits(client);
699         if (ret < 0) {
700                 dev_info(&client->dev, "Not able to set warn and crit limits!\n");
701                 /*Not an error condition, could let the probe continue*/
702         }
703
704         /* set ina3221 to power down mode */
705         ret = __locked_power_down_ina3221(client);
706         if (ret < 0)
707                 goto exit_remove;
708
709         return 0;
710
711 exit_remove:
712         while (i--)
713                 device_remove_file(&client->dev, &ina3221[i].dev_attr);
714 exit_free:
715         devm_kfree(&client->dev, data);
716 exit:
717         return ret;
718 }
719
720 static int ina3221_remove(struct i2c_client *client)
721 {
722         u8 i;
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);
732         return 0;
733 }
734
735 static void ina3221_shutdown(struct i2c_client *client)
736 {
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);
742 }
743
744 #ifdef CONFIG_PM_SLEEP
745 static int ina3221_suspend(struct device *dev)
746 {
747         struct i2c_client *client = to_i2c_client(dev);
748         struct ina3221_data *data = i2c_get_clientdata(client);
749         s32 ret = 0;
750         mutex_lock(&data->mutex);
751         ret = __locked_power_down_ina3221(client);
752         if (ret < 0) {
753                 dev_err(&client->dev,
754                         "INA can't be turned off: 0x%x\n", ret);
755                 goto error;
756         }
757         data->mode = TRIGGERED;
758         data->is_suspended = 1;
759 error:
760         mutex_unlock(&data->mutex);
761         return ret;
762 }
763
764 static int ina3221_resume(struct device *dev)
765 {
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);
773         if (ret < 0)
774                 dev_err(&client->dev,
775                         "INA can't be turned off/on: 0x%x\n", ret);
776
777         data->is_suspended = 0;
778         mutex_unlock(&data->mutex);
779         return ret;
780 }
781 #endif
782
783 static const struct i2c_device_id ina3221_id[] = {
784         {DRIVER_NAME, 0 },
785         {}
786 };
787 MODULE_DEVICE_TABLE(i2c, ina3221_id);
788
789 #ifdef CONFIG_PM_SLEEP
790 static const struct dev_pm_ops ina3221_pm_ops = {
791         SET_SYSTEM_SLEEP_PM_OPS(ina3221_suspend,
792                                 ina3221_resume)
793 };
794 #endif
795
796 static struct i2c_driver ina3221_driver = {
797         .class          = I2C_CLASS_HWMON,
798         .driver = {
799                 .name   = DRIVER_NAME,
800                 .owner = THIS_MODULE,
801 #ifdef CONFIG_PM_SLEEP
802                 .pm = &ina3221_pm_ops,
803 #endif
804         },
805         .probe          = ina3221_probe,
806         .remove         = ina3221_remove,
807         .shutdown       = ina3221_shutdown,
808         .id_table       = ina3221_id,
809 };
810
811 static int __init ina3221_init(void)
812 {
813         return i2c_add_driver(&ina3221_driver);
814 }
815
816 static void __exit ina3221_exit(void)
817 {
818         i2c_del_driver(&ina3221_driver);
819 }
820
821 module_init(ina3221_init);
822 module_exit(ina3221_exit);
823 MODULE_LICENSE("GPL");