blob: 159bbcee882155250149e165c603bc68cd26a715 [file] [log] [blame]
Laxman Dewanganec4664b2016-08-23 13:50:27 +05301/*
2 * Junction temperature thermal driver for Maxim Max77620.
3 *
4 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5 *
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
7 * Mallikarjun Kasoju <mkasoju@nvidia.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 */
13
14#include <linux/irq.h>
15#include <linux/interrupt.h>
16#include <linux/mfd/max77620.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20#include <linux/regmap.h>
21#include <linux/slab.h>
22#include <linux/thermal.h>
23
24#define MAX77620_NORMAL_OPERATING_TEMP 100000
25#define MAX77620_TJALARM1_TEMP 120000
26#define MAX77620_TJALARM2_TEMP 140000
27
28struct max77620_therm_info {
29 struct device *dev;
30 struct regmap *rmap;
31 struct thermal_zone_device *tz_device;
32 int irq_tjalarm1;
33 int irq_tjalarm2;
34};
35
36/**
37 * max77620_thermal_read_temp: Read PMIC die temperatue.
38 * @data: Device specific data.
39 * temp: Temperature in millidegrees Celsius
40 *
41 * The actual temperature of PMIC die is not available from PMIC.
42 * PMIC only tells the status if it has crossed or not the threshold level
43 * of 120degC or 140degC.
44 * If threshold has not been crossed then assume die temperature as 100degC
45 * else 120degC or 140deG based on the PMIC die temp threshold status.
46 *
47 * Return 0 on success otherwise error number to show reason of failure.
48 */
49
50static int max77620_thermal_read_temp(void *data, int *temp)
51{
52 struct max77620_therm_info *mtherm = data;
53 unsigned int val;
54 int ret;
55
56 ret = regmap_read(mtherm->rmap, MAX77620_REG_STATLBT, &val);
57 if (ret < 0) {
58 dev_err(mtherm->dev, "Failed to read STATLBT: %d\n", ret);
59 return ret;
60 }
61
62 if (val & MAX77620_IRQ_TJALRM2_MASK)
63 *temp = MAX77620_TJALARM2_TEMP;
64 else if (val & MAX77620_IRQ_TJALRM1_MASK)
65 *temp = MAX77620_TJALARM1_TEMP;
66 else
67 *temp = MAX77620_NORMAL_OPERATING_TEMP;
68
69 return 0;
70}
71
72static const struct thermal_zone_of_device_ops max77620_thermal_ops = {
73 .get_temp = max77620_thermal_read_temp,
74};
75
76static irqreturn_t max77620_thermal_irq(int irq, void *data)
77{
78 struct max77620_therm_info *mtherm = data;
79
80 if (irq == mtherm->irq_tjalarm1)
81 dev_warn(mtherm->dev, "Junction Temp Alarm1(120C) occurred\n");
82 else if (irq == mtherm->irq_tjalarm2)
83 dev_crit(mtherm->dev, "Junction Temp Alarm2(140C) occurred\n");
84
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -070085 thermal_zone_device_update(mtherm->tz_device,
86 THERMAL_EVENT_UNSPECIFIED);
Laxman Dewanganec4664b2016-08-23 13:50:27 +053087
88 return IRQ_HANDLED;
89}
90
91static int max77620_thermal_probe(struct platform_device *pdev)
92{
93 struct max77620_therm_info *mtherm;
94 int ret;
95
96 mtherm = devm_kzalloc(&pdev->dev, sizeof(*mtherm), GFP_KERNEL);
97 if (!mtherm)
98 return -ENOMEM;
99
100 mtherm->irq_tjalarm1 = platform_get_irq(pdev, 0);
101 mtherm->irq_tjalarm2 = platform_get_irq(pdev, 1);
102 if ((mtherm->irq_tjalarm1 < 0) || (mtherm->irq_tjalarm2 < 0)) {
103 dev_err(&pdev->dev, "Alarm irq number not available\n");
104 return -EINVAL;
105 }
106
Laxman Dewanganec4664b2016-08-23 13:50:27 +0530107 mtherm->dev = &pdev->dev;
108 mtherm->rmap = dev_get_regmap(pdev->dev.parent, NULL);
109 if (!mtherm->rmap) {
110 dev_err(&pdev->dev, "Failed to get parent regmap\n");
111 return -ENODEV;
112 }
113
Johan Hovoldc592faf2017-06-06 17:59:03 +0200114 /*
Johan Hovoldce046e52017-06-06 17:59:04 +0200115 * The reference taken to the parent's node which will be balanced on
116 * reprobe or on platform-device release.
Johan Hovoldc592faf2017-06-06 17:59:03 +0200117 */
Johan Hovoldce046e52017-06-06 17:59:04 +0200118 device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent);
Johan Hovoldc592faf2017-06-06 17:59:03 +0200119
Laxman Dewanganec4664b2016-08-23 13:50:27 +0530120 mtherm->tz_device = devm_thermal_zone_of_sensor_register(&pdev->dev, 0,
121 mtherm, &max77620_thermal_ops);
122 if (IS_ERR(mtherm->tz_device)) {
123 ret = PTR_ERR(mtherm->tz_device);
124 dev_err(&pdev->dev, "Failed to register thermal zone: %d\n",
125 ret);
126 return ret;
127 }
128
129 ret = devm_request_threaded_irq(&pdev->dev, mtherm->irq_tjalarm1, NULL,
130 max77620_thermal_irq,
131 IRQF_ONESHOT | IRQF_SHARED,
132 dev_name(&pdev->dev), mtherm);
133 if (ret < 0) {
134 dev_err(&pdev->dev, "Failed to request irq1: %d\n", ret);
135 return ret;
136 }
137
138 ret = devm_request_threaded_irq(&pdev->dev, mtherm->irq_tjalarm2, NULL,
139 max77620_thermal_irq,
140 IRQF_ONESHOT | IRQF_SHARED,
141 dev_name(&pdev->dev), mtherm);
142 if (ret < 0) {
143 dev_err(&pdev->dev, "Failed to request irq2: %d\n", ret);
144 return ret;
145 }
146
147 platform_set_drvdata(pdev, mtherm);
148
149 return 0;
150}
151
152static struct platform_device_id max77620_thermal_devtype[] = {
153 { .name = "max77620-thermal", },
154 {},
155};
Javier Martinez Canillas3f2883e2016-10-14 11:35:00 -0300156MODULE_DEVICE_TABLE(platform, max77620_thermal_devtype);
Laxman Dewanganec4664b2016-08-23 13:50:27 +0530157
158static struct platform_driver max77620_thermal_driver = {
159 .driver = {
160 .name = "max77620-thermal",
161 },
162 .probe = max77620_thermal_probe,
163 .id_table = max77620_thermal_devtype,
164};
165
166module_platform_driver(max77620_thermal_driver);
167
168MODULE_DESCRIPTION("Max77620 Junction temperature Thermal driver");
169MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
170MODULE_AUTHOR("Mallikarjun Kasoju <mkasoju@nvidia.com>");
171MODULE_LICENSE("GPL v2");